I'm new to coding with Swift 2 and Xcode 7 and I know some people have asked a similar question but I cannot figure it out for my problem. First I have a custom cell that contains a switch and 3 labels. The contents of these cells are read from a 2D array. The array contains a 1 or 0 for the switch position, a number that represents a day of the month and the other 2 represent a money value and a description. The label text changes colour depending on the current date or if the switch is ON or OFF. When the switch in the Cell is toggled the 2D array alters to indicate the change.
The problem I have is as others have seen and that is when you scroll up or down the SWITCH setting or the Label colours change. The contents of the array do not change and the table can be refreshed by moving to another screen and then back again.
I know it is to do with the reuse of cells but I cannot think how I can add this to my code.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return billList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! billTableViewCell
let cellAmountStyle = NSNumberFormatter()
cellAmountStyle.numberStyle = .CurrencyStyle
cell.cellBillDate.text = String(Int(String(billList[indexPath.row][12]))!)
cell.cellBillAmount.text = cellAmountStyle.stringFromNumber(Float(String(billList[indexPath.row][month - 1]))!)
cell.cellBillDescription.text = String(billList[indexPath.row][13])
cell.cellBillPayedOnOff.tag = indexPath.row
cell.delegate = self
if String(billList[indexPath.row][month - 1]) != "0.00" {
if billList[indexPath.row][14] == "0" {
cell.cellBillPayedOnOff.on = false
// Colour Text, RED if past date and Black if OK
if (monthdate >= Int(billList[indexPath.row][12])) {
cell.cellBillDescription.textColor = UIColor.redColor()
}
else {
cell.cellBillDescription.textColor = UIColor.blackColor()
}
}else{
cell.cellBillPayedOnOff.on = true
cell.cellBillDescription.textColor = UIColor.lightGrayColor()
}
}else {
cell.cellBillDescription.textColor = UIColor.lightGrayColor()
cell.cellBillAmount.textColor = UIColor.lightGrayColor()
cell.cellBillDate.textColor = UIColor.lightGrayColor()
}
// Update other screen information
updateAmounts()
return cell
}
Please can somebody help me with this, I'm sure it will help others with similar issues.
Yeah you have clearly identified that this cause because of reusable cell. So answer is you should give color for every element in the cell. The problem is you set colors for only some parts. If you corrected your code like below, you can achieve what you are looking for.
Note
Please give appropriate color for each conditional state. Just give what you should correct.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! billTableViewCell
let cellAmountStyle = NSNumberFormatter()
cellAmountStyle.numberStyle = .CurrencyStyle
cell.cellBillDate.text = String(Int(String(billList[indexPath.row][12]))!)
cell.cellBillAmount.text = cellAmountStyle.stringFromNumber(Float(String(billList[indexPath.row][month - 1]))!)
cell.cellBillDescription.text = String(billList[indexPath.row][13])
cell.cellBillPayedOnOff.tag = indexPath.row
cell.delegate = self
if String(billList[indexPath.row][month - 1]) != "0.00" {
if billList[indexPath.row][14] == "0" {
cell.cellBillPayedOnOff.on = false
// Colour Text, RED if past date and Black if OK
if (monthdate >= Int(billList[indexPath.row][12])) {
cell.cellBillDescription.textColor = UIColor.lightGrayColor()
cell.cellBillAmount.textColor = UIColor.lightGrayColor()
cell.cellBillDate.textColor = UIColor.lightGrayColor()
}
else {
cell.cellBillDescription.textColor = UIColor.lightGrayColor()
cell.cellBillAmount.textColor = UIColor.lightGrayColor()
cell.cellBillDate.textColor = UIColor.lightGrayColor()
}
} else {
cell.cellBillPayedOnOff.on = true
cell.cellBillDescription.textColor = UIColor.lightGrayColor()
cell.cellBillAmount.textColor = UIColor.lightGrayColor()
cell.cellBillDate.textColor = UIColor.lightGrayColor()
}
} else {
cell.cellBillDescription.textColor = UIColor.lightGrayColor()
cell.cellBillAmount.textColor = UIColor.lightGrayColor()
cell.cellBillDate.textColor = UIColor.lightGrayColor()
}
// Update other screen information
updateAmounts()
return cell
}