我正在做一个待办事项。我使用核心数据。在其中我有三个对象。保存成功(SecondViewController
)。但是当我加载表视图控制器时,应用程序立即崩溃。如果我删除detailTextLabel
应用程序正常。
我的核心数据代码:
import Foundation
import CoreData
@objc(Note)
class Note: NSManagedObject {
@NSManaged var mynote: String
@NSManaged var mydate: NSDate
@NSManaged var mytime: String
}
我的表视图控制器代码
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellNote", forIndexPath: indexPath) as! UITableViewCell
if var cellContact = fetchedResultsController?.objectAtIndexPath(indexPath) as? Note {
cell.textLabel!.text = cellContact.mynote
cell.detailTextLabel!.text = cellContact.mytime
}
return cell
答案 0 :(得分:0)
确保您的UITableViewStyle符合详细信息视图类型。使用选项。在展开之前测试您的值。
if let time = cellContact.mytime {
println(time)
// Place a breakpoint here to see if the time value exists first before assigning to detailTextLabel.text
cell.detailTextLabel?.text = time
}
问题在于您的日期转换可能性很大。试试这个
var formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.ShortStyle
formatter.timeStyle = NSDateFormatterStyle.ShortStyle
let timeStamp = formatter.stringFromDate(NSDate())
// Set your mytime here and save