我创建了一个表格视图,一切正常。我想在表格视图中显示1个单元格,标题为#34;你应该添加单元格"。然后,当我向我的数组(tableData)添加信息时,我需要在我的自定义单元格中显示此信息。然后当我删除这个单元格时,我需要显示我的1个单元格标题"你应该添加单元格"。但我不能这样做(请解释我为什么?)
我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
_tableData = [[NSMutableArray alloc] init];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
self.tableData = [NSMutableArray arrayWithArray:[[SOCoreDataManager sharedManager] getAllAims]];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if ([self.tableData count] == 0) {
return 1;
}
return [self.tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)table
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellID = @"cellForMyAims";
if ((int)[self.tableData count] == 0) {
static NSString* cellIDw = @"Cell";
UITableViewCell *cellw = [table dequeueReusableCellWithIdentifier:cellIDw];
if (!cellw) {
cellw = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIDw];
}
cellw.backgroundColor = [UIColor orangeColor];
return cellw;
} else {
CellForMyAimsTableViewCell *cell = [table dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[CellForMyAimsTableViewCell alloc] initWithFrame:CGRectZero];;
}
return cell;
}
}
我删除了:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
SOGoal *goal = self.tableData [indexPath.row];
[[SOCoreDataManager sharedManager] deleteAim:goal];
self.tableData = [NSMutableArray arrayWithArray:[[SOCoreDataManager sharedManager] getAllAims]];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
}
}
错误:
2015-01-14 15:17:25.019 [5332:190142] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:1582
2015-01-14 15:17:25.022 [5332:190142] *** 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 (1) must be equal to the number of rows contained in that section before the update (1), 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 0x00000001050bcf35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000104d55bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001050bcd9a +[NSException raise:format:arguments:] + 106
3 Foundation 0x00000001049725df -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x000000010337bc2a -[UITableView _endCellAnimationsWithContext:] + 11746
5 nameapp 0x00000001029777cf __69-[ListViewController tableView:commitEditingStyle:forRowAtIndexPath:]_block_invoke + 607
6 nameapp 0x000000010297ea10 __86+[SONetworkingWithLegendaryServer deleteAim:legendaryToken:aimId:onSuccess:onFailure:]_block_invoke + 112
7 nameapp 0x000000010296dad8 __64-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke46 + 40
8 libdispatch.dylib 0x0000000106673ba6 _dispatch_call_block_and_release + 12
9 libdispatch.dylib 0x00000001066917f4 _dispatch_client_callout + 8
10 libdispatch.dylib 0x000000010667a8fb _dispatch_main_queue_callback_4CF + 949
11 CoreFoundation 0x0000000105024fe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
12 CoreFoundation 0x0000000104fe7eeb __CFRunLoopRun + 2043
13 CoreFoundation 0x0000000104fe7486 CFRunLoopRunSpecific + 470
14 GraphicsServices 0x00000001070569f0 GSEventRunModal + 161
15 UIKit 0x00000001032a7420 UIApplicationMain + 1282
16 nameapp 0x00000001029c52e3 main + 115
17 libdyld.dylib 0x00000001066c6145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:3)
你得到了NSInternalInconsistencyException
,因为你的表总是至少有一行,但是当你滑动删除你告诉它的最后一行时,你最终会得到0个单元格。这种明显的不匹配会产生异常。
要修复它,tableView:commitEditingStyle:forRowAtIndexPath:
需要检查“用户删除了最后一行”的情况并以不同的方式处理它。当你在最后一行时,你有两个选择:
刷新现有索引路径(它应转换为占位符单元格):
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
删除用户的单元格并插入占位符单元格:
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];