每当我有我的UITableView
中的数据,我开始删除,它工作正常。但是,当我到达表中的最后一个对象并删除它时,它会崩溃。NSInternalInconsistencyException(无效的行数)
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).'
以下是我正在做的东西编辑:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
if ([myData count] >= 1) {
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myData removeObjectAtIndex:[indexPath row]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"];
[myData writeToFile:somepath atomically:YES];
[table reloadData];
if ([myData count] == 0) {
[tableView endUpdates];
[tableView reloadData];
}
else {
[tableView endUpdates];
}
}
}
}
而且也是这样:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if ([myData count] != 0) {
return [myData count];
}
else {
return 1;
}
}
我回到1的原因是因为我正在做在cellForRowAtIndexPath
中表示“没有数据保存”的单元格。这就是我的意思:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if ([cityData count] != 0) {
//normal setup removed for clarity
}
else {
cell.textLabel.text = @"No saved data!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag = 1;
return cell;
}
}
那么,我在做错误的编辑代码中得到这个错误?谢谢!
完美,谢谢! – 2011-03-28 05:00:36
你也可以做的只是调用tableView deleteRowsAtIndexPaths,如果[数据计数]> 0,然后DO调用reloadData。 这样你就不会删除最后一个单元,如果它将被用于“无数据”单元,并且重载会确保委托在桌面视图中加载正确的信息。 – EeKay 2012-08-30 10:09:02
谢谢,这对我也有帮助! – 2013-06-02 18:33:15