加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iOS8 Swift:deleteRowsAtIndexPaths崩溃

发布时间:2020-12-15 01:42:25 所属栏目:百科 来源:网络整理
导读:我在使用Swift,iOS 8,Xcode 6 Beta 6中的tableView中删除一行时遇到一些麻烦.每当我尝试删除一行时,我都会遇到错误. Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3302.3.1/UITableView.m:1581 2014-
我在使用Swift,iOS 8,Xcode 6 Beta 6中的tableView中删除一行时遇到一些麻烦.每当我尝试删除一行时,我都会遇到错误.

Assertion failure in -[UITableView _endCellAnimationsWithContext:],
/SourceCache/UIKit_Sim/UIKit-3302.3.1/UITableView.m:1581 2014-08-30
20:31:00.971 Class Directory[13290:3241692] *** Terminating app due to
uncaught exception ‘NSInternalInconsistencyException’,reason:
‘Invalid update: invalid number of rows in section 1. The number of
rows contained in an existing section after the update (25) must be
equal to the number of rows contained in that section before the
update (25),plus or minus the number of rows inserted or deleted from
that section (0 inserted,1 deleted) and plus or minus the number of
rows moved into or out of that section (0 moved in,0 moved out).

我已经阅读了这个常见问题的所有答案,并且觉得我已经满足了建议的条件.该项似乎从数据模型中删除 – 当我重新加载应用程序时,删除的项目从表中消失 – 但似乎在相应的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()
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读