当我滚动我的表格视图时,我的单元格的一些文本会改变颜色

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

当我编译我的代码时,我没有错误并且结果很好但是当我在我的表格视图中滚动时一些单元格变成红色。 目的是用红色显示所有超过截止日期的任务

我认为问题出在 cellForRowAtIndexPath 中:

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

}

这是我 class 表视图的 cellForRowAtIndexPath 中的代码:

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
}

这是我的 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
        }
    }

}

您引用的代码中需要一个 else。当 dateIsGreaterThanDate 测试为假时,你希望它是什么颜色?