我正在使用Swift开发一个基于表视图的应用程序。我一整天都在尝试删除tableview行,但我一直得到这个异常。不知道这是什么。这里的代码我必须删除行。不知道还能做些什么来诚实。这应该是一个简单的任务,Swift应该让我们更容易,但这真的是我的阻碍。
代码:override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
if editingStyle == UITableViewCellEditingStyle.Delete {
// do something with unwrappedIndexPath.row
context.deleteObject(myData.objectAtIndex(indexPath.row) as NSManagedObject)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
var error: NSError? = nil
if !context.save(&error) {
abort()
}
}
}
埃罗:
(
"<MyData: 0x10bba1a90> (entity: MyData; id: 0xd0000000001c0000 <x-coredata://8A0231EE-899F-48F9-953B-CAB1252FD1D0/MyData/p7> ; data: <fault>)"
)
<MyData: 0x10d84ba20> (entity: MyData; id: 0xd000000000300000 <x-coredata://8A0231EE-899F-48F9-953B-CAB1252FD1D0/MyData/p12> ; data: {
name = test;
skills = te;
})
Data Saved
(
"<MyData: 0x10bba1a90> (entity: MyData; id: 0xd0000000001c0000 <x-coredata://8A0231EE-899F-48F9-953B-CAB1252FD1D0/MyData/p7> ; data: {n name = Rob;n skills = Dev;n})",
"<MyData: 0x10bf1dec0> (entity: MyData; id: 0xd000000000300000 <x-coredata://8A0231EE-899F-48F9-953B-CAB1252FD1D0/MyData/p12> ; data: <fault>)"
)
2014-06-13 21:51:51.452 coreDataApp[2090:38878] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3185.20/UITableView.m:1487
2014-06-13 21:51:51.487 coreDataApp[2090:38878] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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).'
*** First throw call stack:
(
0 CoreFoundation 0x0000000100848e35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001022f49a0 objc_exception_throw + 45
2 CoreFoundation 0x0000000100848c9a +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000100cdb6fb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x0000000101190b3b -[UITableView _endCellAnimationsWithContext:] + 11785
5 coreDataApp 0x000000010000454c _TFC11coreDataApp23dataTableViewController9tableViewfS0_FTGSQCSo11UITableView_18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathGSQCSo11NSIndexPath__T_ + 2332
6 coreDataApp 0x0000000100004927 _TToFC11coreDataApp23dataTableViewController9tableViewfS0_FTGSQCSo11UITableView_18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathGSQCSo11NSIndexPath__T_ + 103
7 UIKit 0x00000001011b1e51 -[UITableView animateDeletionOfRowWithCell:] + 114
8 UIKit 0x00000001013409db -[UITableViewCell _swipeDeleteButtonPushed] + 60
9 UIKit 0x00000001010c4333 -[UIApplication sendAction:to:from:forEvent:] + 75
10 UIKit 0x00000001011c086d -[UIControl _sendActionsForEvents:withEvent:] + 467
11 UIKit 0x00000001011bfc3f -[UIControl touchesEnded:withEvent:] + 522
12 UIKit 0x000000010144efb7 _UIGestureRecognizerUpdate + 7670
13 UIKit 0x0000000101105d66 -[UIWindow _sendGesturesForEvent:] + 1041
14 UIKit 0x0000000101106993 -[UIWindow sendEvent:] + 667
15 UIKit 0x00000001010d7dd1 -[UIApplication sendEvent:] + 246
16 UIKit 0x00000001010e2dda _UIApplicationHandleEventFromQueueEvent + 11315
17 UIKit 0x00000001010bfc5f _UIApplicationHandleEventQueue + 1461
18 CoreFoundation 0x000000010077fbd1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19 CoreFoundation 0x000000010077522d __CFRunLoopDoSources0 + 269
20 CoreFoundation 0x000000010077485d __CFRunLoopRun + 861
21 CoreFoundation 0x0000000100774296 CFRunLoopRunSpecific + 470
22 GraphicsServices 0x0000000104462ce4 GSEventRunModal + 161
23 UIKit 0x00000001010c3308 UIApplicationMain + 2994
24 coreDataApp 0x00000001000096ad top_level_code + 77
25 coreDataApp 0x00000001000096ea main + 42
26 libdyld.dylib 0x0000000102852145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
忘记从数据模型中删除对象
myData.removeObjectAtIndex(indexPath!.row)
这不是Swift的问题,在Obj-C中也会发生。
通过在展开前测试两个可选选项(tableView和indexPath)来实现更安全的方法:
func tableView(tableView: UITableView?, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath?) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
if editingStyle == UITableViewCellEditingStyle.Delete {
// do something with unwrappedIndexPath.row
if let ind = indexPath? {
context.deleteObject(myData.objectAtIndex(ind.row) as NSManagedObject)
tableView?.deleteRowsAtIndexPaths([ind], withRowAnimation: .Automatic)
}
var error: NSError? = nil
if !context.save(&error) {
abort()
}
}
}
在tableView数据操作中,顺序很重要。首先从数据模型中删除对象,然后删除行。
myData.removeObjectAtIndex(indexPath!.row)
tableView?.deleteRowsAtIndexPaths([ind], withRowAnimation: .Automatic)
这个线程有点旧了,但是为了帮助其他人从搜索中得到帮助,在swift 4中,这看起来像这样:
myData.remove(indexPath.row)
tableView?.deleteRows(at: [indexPaths], with: .Automatic)