2015-12-01 21 views
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不能用于批量更新吗?有什么我可以做更新单元格吗?

回答

1

观看2014年WWDC上关于批量更新的视频。基本上更新是在磁盘上进行的,但是您的内存中的数据副本是不是已更新。您必须自己更新内存中的对象,可以重置上下文或单独刷新每个对象。

+0

完美,谢谢! – RNDMizer