2016-04-08 53 views
0

我嘗試改變labelcolor採取的顏色,如果在我的detailTextLabel.text「約會」前輩那麼今天..xcode swift cell detailTextLabel.text ?.從日期

 // Configure the cell... 

    cell.textLabel?.textColor = UIColor.blueColor() 
    cell.detailTextLabel?.textColor = UIColor.darkGrayColor() 

    let item = frc.objectAtIndexPath(indexPath) as! Item 
    cell.textLabel?.text = item.name! + "-" + item.etage! + "-" + item.raum! 
    let note = item.date 
    cell.detailTextLabel!.text = "\(note!)" 

    if note == "01.04.2016 23:00" { 
    cell.detailTextLabel?.textColor = UIColor.redColor() 
    } else { 

    } 

    return cell 
} 

我怎樣才能做到這一點?它適用於特定的日期,但不適用於NSDate()或類似的東西。我瘋了!

大thx!

+0

檢查這個http://stackoverflow.com/questions/26807416/check-if-date-is-before-current-date-swift和請確保與相同的格式 –

+0

比較顯然你的Item.date是字符串格式,所以不可能直接與NSDate對象進行比較。 – sleepwalkerfx

回答

0

正如指出there可以讓這樣的事情:

... 
let note = item.date 
let now = NSDate() 

if note.earlierDate(now).isEqualToDate(note) { 
    cell.detailTextLabel?.textColor = UIColor.redColor() 
} 
+0

thx =)但現在我有一條消息:類型'String?'的值has no member'earlierDate' – slevin

+0

在代碼中使用[this](http://stackoverflow.com/a/29319732/3466461)擴展 – giacavicchioli

+0

應該驗證答案是否有效。 – giacavicchioli