- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 Swift 的同时构建一个博客阅读器。在此,我将数据保存到核心数据,并可以选择将文章标记为“已读”。当我使用 editActionsForRowsAtIndexPath 将它们单独标记为已读时,tableview.reloadData() 起作用并且单元格格式发生变化。
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("Updated") 以验证它不是导致问题的 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 不能用于批量更新吗?我还能做些什么来更新单元格吗?
最佳答案
观看 2014 年 WWDC 有关批量更新的视频。基本上,更新是在磁盘上进行的,但内存中的数据副本未更新。您必须自己更新内存中的对象,要么重置上下文,要么单独刷新每个对象。
关于swift - NSBatchUpdateRequest : . 更新未在 didChangeObject 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012007/
我正在尝试使用 NSBatchUpdateRequest 来更新一对多关系。 我的实体Presentation有多个Slide,是另一个实体,它们是反比关系。 当我尝试更新幻灯片时,出现此错误: **
我在尝试执行 NSBatchUpdateRequest 时不断收到以下错误。 *** Terminating app due to uncaught exception 'NSInternalInco
目前,我通过以下代码执行多个更新操作。 func updateOrders(_ updates : [(objectID: NSManagedObjectID, order: Int64)]) {
我正在学习 Swift 的同时构建一个博客阅读器。在此,我将数据保存到核心数据,并可以选择将文章标记为“已读”。当我使用 editActionsForRowsAtIndexPath 将它们单独标记为已
长话短说 显然,当通过执行 NSBatchUpdateRequest 发生更改时,NSFetchedResultsController 不知道给定 NSManagedObjectContext 上的数
这是我的NSBatchUpdateRequest: let batchUpdateRequest = NSBatchUpdateRequest(entityName: "WLItem")
是否可以使用 NSBatchUpdateRequest 将属性设置为 nil?将 NSNull() 传递给 propertiesToUpdate 无效: let unlockRequest = NSB
我创建了简单的 NSBatchUpdateRequest: let bur = NSBatchUpdateRequest(entityName: "WLWishlist") bur.predicate
我得到了一个 NSFetchedResultsController,它是我使用 NSManagedObjectContext 设置的。我使用此上下文执行提取。 我还有一个 NSBatchUpdateR
是否可以使用NSBatchUpdateRequest将属性设置为nil?将NSNull()传递给PropertiesToUpdate不起作用:。我没有收到任何错误,但它没有清除日期。。我还尝试将其设置
我是一名优秀的程序员,十分优秀!