无法删除部分中的行

时间:2015-01-21 20:56:25

标签: iphone swift core-data ios8 xcode6.1

我通过删除部分中的单元格来解决问题.tableViewController有三个部分,包含各种单元格。如果我尝试删除一个单元格,调试器将显示:

  

2015-01-23 20:22:15.105等级 - Zensurenverwaltung [23854:5674475] *断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-3318.16.14 / UITableView.m: 1566   2015-01-23 20:22:15.133等级 - Zensurenverwaltung [23854:5674475] * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:无效的节数。更新后的表视图中包含的节数(3)必须等于更新前的表视图中包含的节数(3),加上或减去插入或删除的节数(0插入,1删除)。”   ***第一次抛出调用堆栈:   (       0 CoreFoundation 0x00000001008bef35 exceptionPreprocess + 165       1 libobjc.A.dylib 0x00000001025adbb7 objc_exception_throw + 45       2 CoreFoundation 0x00000001008bed9a + [NSException raise:format:arguments:] + 106       3基础0x0000000100d565df - [NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195       4 UIKit 0x00000001013c98ff - [UITableView _endCellAnimationsWithContext:] + 10935       5年级 - Zensurenverwaltung 0x00000001000fa0c2 _TFC26Grade___Zensurenverwaltung28TestTypesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 3618       6年级 - Zensurenverwaltung 0x00000001000fa207 _TToFC26Grade___Zensurenverwaltung28TestTypesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 87       7 UIKit 0x00000001013edcb4 - [UITableView animateDeletionOfRowWithCell:] + 130       8 UIKit 0x00000001013ce125 __52- [UITableView _swipeActionButtonsForRowAtIndexPath:] _ block_invoke + 72       9 UIKit 0x00000001012f68be - [UIApplication sendAction:to:from:forEvent:] + 75       10 UIKit 0x00000001013fd410 - [UIControl _sendActionsForEvents:withEvent:] + 467       11 UIKit 0x00000001013fc7df - [UIControl touchesEnded:withEvent:] + 522       12 UIKit 0x00000001016a3540 _UIGestureRecognizerUpdate + 9487       13 UIKit 0x000000010133bff6 - [UIWindow _sendGesturesForEvent:] + 1041       14 UIKit 0x000000010133cc23 - [UIWindow sendEvent:] + 667       15 UIKit 0x00000001013099b1 - [UIApplication sendEvent:] + 246       16 UIKit 0x0000000101316a7d _UIApplicationHandleEventFromQueueEvent + 17370       17 UIKit 0x00000001012f2103 _UIApplicationHandleEventQueue + 1961       18 CoreFoundation 0x00000001007f4551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17       19 CoreFoundation 0x00000001007ea41d __CFRunLoopDoSources0 + 269       20 CoreFoundation 0x00000001007e9a54 __CFRunLoopRun + 868       21 CoreFoundation 0x00000001007e9486 CFRunLoopRunSpecific + 470       22 GraphicsServices 0x00000001052fd9f0 GSEventRunModal + 161       23 UIKit 0x00000001012f5420 UIApplicationMain + 1282       24年级 - Zensurenverwaltung 0x00000001001e164e top_level_code + 78       25年级 - Zensurenverwaltung 0x00000001001e168a主要+ 42       26 libdyld.dylib 0x0000000102d87145 start + 1       27 ??? 0x0000000000000001 0x0 + 1   )   libc ++ abi.dylib:以NSException类型的未捕获异常终止

我只是不明白......

这是我的代码:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    var indexes: NSMutableIndexSet = NSMutableIndexSet()
    dataTestType = context.executeFetchRequest(fetchRequestForTestType, error: nil) as [TestType]

    if editingStyle == UITableViewCellEditingStyle.Delete {

        context.deleteObject(dataTestType[indexPath.row] as NSManagedObject)
        context.save(nil)
        dataTestType.removeAtIndex(indexPath.row)

        if dataTestType.count == 0 {
            indexes.addIndex(indexPath.section)
        }

        tableView.beginUpdates()
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        tableView.deleteSections(indexes, withRowAnimation: .Fade)
        tableView.endUpdates()
    }
}

任何人都可以帮助我吗?

非常感谢你!

1 个答案:

答案 0 :(得分:1)

您从indexPath.row数组

中删除dataTestType处的对象
 dataTestType.removeAtIndex(indexPath.row)

在下一行中,您尝试将对象放在同一索引处,但如果删除了数组中唯一的对象,它将不存在:

context.deleteObject(dataTestType[indexPath.row] as NSManagedObject)

您可以通过重新排序这两行来解决问题。

正如matt指出的那样,您也可以使用NSFetchedResultsController,因为建议在UITableView中显示核心数据记录。那么你根本不需要dataTestType数组。现在,每次调用context.executeFetchRequest方法时都会调用commitEditingStyle。如果要获取许多记录,效率很低,可能会导致性能不佳。