每當我有我的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