0
我在學習Swift的同時構建了一個博客閱讀器。在此我將數據保存到核心數據並可選擇將文章標記爲「已讀」。當我使用editActionsForRowsAtIndexPath將它們分別標記爲read時,tableview.reloadData()將起作用,並且單元格格式會發生變化。NSBatchUpdateRequest:.Update在didChangeObject中沒有觸發
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let object = fetchedResultsController.objectAtIndexPath(indexPath)
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
var edit = UITableViewRowAction()
if let read = object.valueForKey("read") as? Bool{
if read == true{
edit = UITableViewRowAction(style: .Normal, title: "Mark\nUnread") { action, index in
object.setValue(false, forKey: "read")
do{
try context.save()
}catch{
}
tableView.reloadData()
self.updateReadAll()
}
edit.backgroundColor = UIColor(hexaString: "#A1A19E")
}else{
edit = UITableViewRowAction(style: .Normal, title: "Mark\nRead") { action, index in
object.setValue(true, forKey: "read")
do{
try context.save()
}catch{
}
tableView.reloadData()
self.updateReadAll()
}
edit.backgroundColor = UIColor(hexaString: "#A1A19E")
}
return [edit]
}else{
return []
}
}
但是,當我使用NSBatchUpdateRequest核心數據更新但單元格格式不更新。我必須強制退出應用程序並重新啓動單元格才能正確顯示。我在didChangeObject中添加了print(「更新」)以驗證它不是造成問題的configureCell。
@IBAction func markReadAction(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
let table = NSBatchUpdateRequest(entityName: "BlogItems")
table.propertiesToUpdate = ["read":true]
do{
try context.executeRequest(table)
}catch{
}
updateReadAll()
tableView.reloadData()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
print("Updated")
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
}
}
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
cell.textLabel!.text = object.valueForKey("title")!.description
cell.detailTextLabel!.text = object.valueForKey("snippet")!.description
view.backgroundColor = UIColor(hexaString: "#F8F7F5")
cell.textLabel!.textColor = UIColor.blackColor()
cell.detailTextLabel!.textColor = UIColor.blackColor()
if let read = object.valueForKey("read") as? Bool{
if read == true{
cell.textLabel!.textColor = UIColor(hexaString: "#A1A19E")
cell.detailTextLabel!.textColor = UIColor(hexaString: "#A1A19E")
}
}
}
func updateReadAll(){
if searchCoreData(NSPredicate(format: "read = %@", false)) > 0{
markReadView.enabled = true
markReadView.tintColor = UIColor.blackColor()
}else{
markReadView.enabled = false
markReadView.tintColor = UIColor(hexaString: "#A1A19E")
}
}
didChangeObject不能用於批量更新嗎?有什麼我可以做更新單元格嗎?
完美,謝謝! – RNDMizer