iOS8 Swift:deleteRowsAtIndexPaths崩溃
我在使用Swift,iOS 8,Xcode 6 Beta 6中的tableView中删除一行时遇到一些麻烦.每当我尝试删除一行时,我都会遇到错误.
我已经阅读了这个常见问题的所有答案,并且觉得我已经满足了建议的条件.该项似乎从数据模型中删除 – 当我重新加载应用程序时,删除的项目从表中消失 – 但似乎在相应的sqlite文件中有一些残余,当然数学不加.吐出indexPath的println显示正确的Section和Row.我很困惑.这应该是直截了当的但我遗漏了一些愚蠢的我确定,我怀疑在数据模型删除.完整项目于Github. func numberOfSectionsInTableView(tableView: UITableView!) -> Int { return fetchedResultController.sections.count } func tableView(tableView: UITableView!,numberOfRowsInSection section: Int) -> Int { return fetchedResultController.sections[section].numberOfObjects } func tableView(tableView: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let cell = tableViewMain.dequeueReusableCellWithIdentifier("CellMain",forIndexPath: indexPath) as UITableViewCell let personForRow = fetchedResultController.objectAtIndexPath(indexPath) as Person cell.textLabel.text = personForRow.fullName() return cell } func tableView(tableView: UITableView!,canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool { return true } func tableView(tableView: UITableView!,editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle { return UITableViewCellEditingStyle.Delete } func tableView(tableView: UITableView!,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath!) { println("section and row (indexPath.section) (indexPath.row) ") if (editingStyle == UITableViewCellEditingStyle.Delete) { let personForRow : NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as Person context?.deleteObject(personForRow) context?.save(nil) tableViewMain.beginUpdates() tableViewMain.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Fade) tableViewMain.endUpdates() } 解决方法
使用Xcode Core Data Master-Detail模板项目很容易重现崩溃.作为一般规则,当您使用NSFetchedResultsController时,您应该使用NSFetchedResultsControllerDelegate(您已声明它但不使用它).
删除tableView中的那些行:commitEditingStyle:forRowAtIndexPath:method: tableViewMain.beginUpdates() tableViewMain!.deleteRowsAtIndexPaths([indexPath!],withRowAnimation: UITableViewRowAnimation.Fade) tableViewMain.endUpdates() 并将这些行添加到viewController类: func controllerWillChangeContent(controller: NSFetchedResultsController) { tableViewMain.beginUpdates() } func controller(controller: NSFetchedResultsController!,didChangeSection sectionInfo: NSFetchedResultsSectionInfo!,atIndex sectionIndex: Int,forChangeType type: NSFetchedResultsChangeType) { switch type { case .Insert: tableViewMain.insertSections(NSIndexSet(index: sectionIndex),withRowAnimation: .Fade) case .Delete: tableViewMain.deleteSections(NSIndexSet(index: sectionIndex),withRowAnimation: .Fade) default: return } } func controller(controller: NSFetchedResultsController!,didChangeObject anObject: AnyObject!,atIndexPath indexPath: NSIndexPath!,forChangeType type: NSFetchedResultsChangeType,newIndexPath: NSIndexPath!) { switch type { case .Insert: tableViewMain.insertRowsAtIndexPaths([newIndexPath],withRowAnimation: .Fade) case .Delete: tableViewMain.deleteRowsAtIndexPaths([indexPath],withRowAnimation: .Fade) case .Update: return //Should also manage this case!!! //self.configureCell(tableView.cellForRowAtIndexPath(indexPath),atIndexPath: indexPath) case .Move: tableViewMain.deleteRowsAtIndexPaths([indexPath],withRowAnimation: .Fade) tableViewMain.insertRowsAtIndexPaths([newIndexPath],withRowAnimation: .Fade) default: return } } func controllerDidChangeContent(controller: NSFetchedResultsController!) { tableViewMain.endUpdates() } 这应该可以解决您的问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |