Search code examples
iosuitableviewcolorscell

some text of my cells changes color when I scroll my tableview


when I compile my code, i have no error and the result is good but when i scroll in my tableview some cells become in red color. the purpose is to display in red color all the taks which had passed the due date

I think the problem is here in the cellForRowAtIndexPath:

let dateNow = NSDate()
if (dateNow.dateIsGreaterThanDate(date!))
{
    cell.colorLabels = UIColor.redColor()

}

This my code in my cellForRowAtIndexPath of my class tableview:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! cellTabOfTasks
    let row = indexPath.row
    let section = indexPath.section
    var dueDateFormat = NSDateFormatter()
    var dueDateFormat2 = NSDateFormatter()
    dueDateFormat2.dateFormat = "yyyy-MM-dd HH:mm"
    dueDateFormat.dateStyle = NSDateFormatterStyle.LongStyle
    var date = dueDateFormat2.dateFromString(tabTask[section].accounts[row].dueDateActivity)
    let date2 = dueDateFormat.stringFromDate(date!)


    let dateNow = NSDate()
    if (dateNow.dateIsGreaterThanDate(date!))
    {
        cell.colorLabels = UIColor.redColor()

    }

    cell.subject.text = tabTask[section].accounts[row].subjectActivity
    cell.status.text = tabTask[section].accounts[row].statusActivity
    cell.priority.text = tabTask[section].accounts[row].priorityActivity
    cell.dueDate.text = date2


    return cell
}

this is my code of my class tableviewcell :

class cellTabOfTasks: UITableViewCell {

    @IBOutlet var subject: UILabel!
    @IBOutlet var dueDate: UILabel!
    @IBOutlet var status: UILabel!
    @IBOutlet var priority: UILabel!

    var colorLabels: UIColor = UIColor.blackColor() {
        didSet {
            subject?.textColor = colorLabels
            dueDate?.textColor = colorLabels
            status?.textColor = colorLabels
            priority?.textColor = colorLabels
        }
    }

}

Solution

  • You need an else in the code you quoted. What colour do you want it to be when the dateIsGreaterThanDate test is false?