iOS8 Swift:deleteRowsAtIndexPaths崩溃

iOS8 Swift:deleteRowsAtIndexPaths崩溃,第1张

概述我在使用Swift,iOS 8,Xcode 6 Beta 6中的tableView中删除一行时遇到一些麻烦.每当我尝试删除一行时,我都会遇到错误. Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3302.3.1/UITableView.m:1581 2 我在使用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()}

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

总结

以上是内存溢出为你收集整理的iOS8 Swift:deleteRowsAtIndexPaths崩溃全部内容,希望文章能够帮你解决iOS8 Swift:deleteRowsAtIndexPaths崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066212.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存