小编典典

iOS8 Swift:deleteRowsAtIndexPaths崩溃

swift

在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()
    }

阅读 396

收藏
2020-07-07

共1个答案

小编典典

使用Xcode Core Data Master-
Detail模板项目很容易重现崩溃。通常,当您使用时NSFetchedResultsController,您应该真正使用NSFetchedResultsControllerDelegate(您已声明但不使用它)。

删除tableView:commitEditingStyle:forRowAtIndexPath:方法中的这些行:

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()
}

这应该可以解决您的问题。

2020-07-07