I have a segmented control which looks like this:
With the following code in cellForRowAt:
switch cell.methodType.selectedSegmentIndex{
case 0: expense.cash = true && expense.credit != true
case 1: expense.credit = true && expense.cash != true
default:
break
}
When I click on the other 3 columns, I select an option and I get directed back to this view controller which gets populated like below.
However, at random times, the segmented control can switch from credit to cash and I have to reselect credit. I ave no idea why this is happening. It happens sometimes, and doesn't happen others. Whenever this happens, my expense type switches from credit to cash and it doesn't register as credit. I have the following code in my cellForRowAt in another view controller where all the expenses get saved which looks like this:
if expense.cash && expense.expense{
print("cash")
cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Expense Icon")
}
else if expense.cash && expense.income{
print("cash")
cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Income Icon")
}
else if expense.credit && expense.income{
print("credit")
cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Income Icon")
}
else if expense.credit && expense.income{
print("credit")
cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Expense Icon")
}
If the segmented control doesn't change while I click the 3 other rows, then it registers as "credit", but if it randomly decides to change to cash, then the expense registers as "cash".
Here is the code for my whole cellForAtIndexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath) as! ListDataTableViewCell
if indexPath.row == 0{
cell.dataTitleLabel.text = "Category"
cell.methodType.isHidden = true
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.optionSelectedLabel.isHidden = false
cell.optionSelectedLabel.text! = categoryDisplayed
cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
} else if indexPath.row == 1{
cell.dataTitleLabel.text = "Method"
cell.methodType.isHidden = false
cell.methodType.tintColor = UIColor(red:0.29, green:0.68, blue:0.71, alpha:1.0)
cell.optionSelectedLabel.isHidden = true
cell.accessoryType = .none
cell.selectionStyle = .none
} else if indexPath.row == 2{
cell.dataTitleLabel.text = "Currency"
cell.methodType.isHidden = true
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.optionSelectedLabel.isHidden = false
cell.optionSelectedLabel.text! = currencyDisplayed
cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
cell.optionSelectedLabel.minimumScaleFactor = 0.2
} else if indexPath.row == 3{
cell.dataTitleLabel.text = "Collection"
cell.methodType.isHidden = true
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.optionSelectedLabel.isHidden = false
cell.optionSelectedLabel.text! = collectionsDisplayed
cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
}
return cell
}
I tried updating the code to this
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath) as! ListDataTableViewCell
let cell2 = tableView.dequeueReusableCell(withIdentifier: "listCell2", for: indexPath) as! ListDataTableViewCell2
if indexPath.row == 0{
cell.dataTitleLabel.text = "Category"
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.optionSelectedLabel.isHidden = false
cell.optionSelectedLabel.text! = categoryDisplayed
cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
return cell
} else if indexPath.row == 1{
cell2.dataLabel.text = "Method"
cell2.methodType.isHidden = false
cell2.methodType.tintColor = UIColor(red:0.29, green:0.68, blue:0.71, alpha:1.0)
cell2.accessoryType = .none
cell2.selectionStyle = .none
return cell2
} else if indexPath.row == 2{
cell.dataTitleLabel.text = "Currency"
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.optionSelectedLabel.isHidden = false
cell.optionSelectedLabel.text! = currencyDisplayed
cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
cell.optionSelectedLabel.minimumScaleFactor = 0.2
return cell
} else if indexPath.row == 3{
cell.dataTitleLabel.text = "Collection"
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.optionSelectedLabel.isHidden = false
cell.optionSelectedLabel.text! = collectionsDisplayed
cell.optionSelectedLabel.adjustsFontSizeToFitWidth = true
return cell
}
else {
return cell
}
}
but am greeted with a SIGABRT error in the line which declares the cell2