Nat*_*olz 9 xcode uitableview ios swift ios8
我在使用Swift,iOS 8,Xcode 6 Beta 6中的tableView中删除一行时遇到了一些麻烦.每当我尝试删除一行时,我都会遇到错误.
断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache/UIKit_Sim/UIKit-3302.3.1/UITableView.m:1581 2014-08-30 20:31:00.971类目录[13290:3241692]***终止应用程序未捕获的异常'NSInternalInconsistencyException',原因:'无效更新:第1节中的无效行数.更新后现有部分中包含的行数(25)必须等于更新前该部分中包含的行数(25),加上或减去从该部分插入或删除的行数(插入0,删除1),加上或减去移入或移出该部分的行数(0移入,0移出).
我已经阅读了这个常见问题的所有答案,并且觉得我已经满足了建议的条件.该项似乎从数据模型中删除 - 当我重新加载应用程序时,已删除的项目从表中消失 - 但在相应的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()
}
Run Code Online (Sandbox Code Playgroud)
使用Xcode Core Data Master-Detail模板项目很容易重现崩溃.作为一般规则,当您使用时NSFetchedResultsController,您应该使用NSFetchedResultsControllerDelegate(您已声明但不使用它).
删除tableView:commitEditingStyle:forRowAtIndexPath:方法中的那些行:
tableViewMain.beginUpdates()
tableViewMain!.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
tableViewMain.endUpdates()
Run Code Online (Sandbox Code Playgroud)
并将这些行添加到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()
}
Run Code Online (Sandbox Code Playgroud)
这应该可以解决您的问题.
| 归档时间: |
|
| 查看次数: |
12777 次 |
| 最近记录: |