【发布时间】:2016-02-29 20:39:36
【问题描述】:
我已经搜索过如何做到这一点,但答案对我不起作用,所以我再次询问。 在我的应用程序中,用户添加了任务和截止日期。如果截止日期已从当前日期过去,我想将名为“pendenciaLbl”(在单元格类中)的标签更改为“迟到”,并将截止日期标签更改为“已过截止日期”,以便用户知道截止日期已过。 我用来这样做的代码是这样的:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("AtivCell") as? AtivCell {
cell.configureCell(materias[indexPath.row], date: dates[indexPath.row], descricao: descricoes[indexPath.row], pendencia: pendencia[indexPath.row], categoria: categorias[indexPath.row])
let calendar = NSCalendar.currentCalendar()
let dateFormatter = NSDateFormatter()
let currentDate = NSDate()
let flags = NSCalendarUnit.Day
if let transformedDate = dateFormatter.dateFromString(dates[indexPath.row]) {
let components = calendar.components(flags, fromDate: transformedDate, toDate: currentDate, options: [])
if components.day >= 1 {
dates[indexPath.row] = "Passes due date"
cell.pendenciaLbl.text = "Late"
cell.pendenciaLbl.textColor = UIColor.blueColor()
}
}
return cell
} else {
return AtivCell()
}
}
问题是这段代码不起作用,我不知道为什么。我已经通过添加已经过去的日期(如昨天)进行了测试,但标签仍然相同。我将不胜感激。
重要提示:日期[indexPath] 的格式为“MMM, dd, yyyy”,例如 2016 年 2 月 29 日。
【问题讨论】:
-
将 NSDate 对象存储在数据层中并将它们转换为 NSString 仅用于显示可能是个好主意 - 这将简化数据处理。