【发布时间】:2016-07-21 22:45:04
【问题描述】:
我是 iOS 开发的新手,我正在创建我的第一个应用程序,如果问题真的很糟糕,我很抱歉。我在 Xcode 中使用 Master/Detail 模板并使用 Core Data。我将模板更改为在 tableView 中使用自定义单元格样式。当用户想要添加一些东西时,他们会切换到另一个视图以完成表单。当用户单击 Save 时,表单数据被保存到 Core Data,然后视图返回 MasterView。
我的问题是,当保存表单时,tableView 会更新以显示新单元格,但它会间歇性地为空白tableView。有时这会发生,有时不会发生。在从应用切换器关闭应用并重新打开之前,该单元格将保持空白。
有什么我忘记更改的吗?感谢您的帮助。
以下是我在模板中更改的功能以添加我的自定义单元格样式:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomTableViewCell
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
self.configureCell(cell, withObject: object)
return cell
}
func configureCell(cell: CustomTableViewCell, withObject object: NSManagedObject) {
let imageData: NSData = object.valueForKey("image") as! NSData
let picture: UIImage = UIImage(data: imageData)!
let date: NSDate
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
cell.cellImage.image = picture
cell.locationNameLabel!.text = object.valueForKey("name")!.description
cell.categoryLabel!.text = object.valueForKey("category")!.description
date = object.valueForKey("date")! as! NSDate
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
cell.dateLabel!.text = dateFormatter.stringFromDate(date)
}
这是我为 FetchedResultsController 更改的唯一函数
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)! as! CustomTableViewCell, withObject: anObject as! NSManagedObject)
case .Move:
tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
}
}
【问题讨论】:
标签: ios swift uitableview core-data