【发布时间】:2011-05-13 17:32:41
【问题描述】:
当我使用 commitEditingStyle 从 UITableView 中删除一行时,我的应用程序崩溃并显示以下错误消息。奇怪的是我从第 3 节中删除。根据消息的不一致来自第 4 节。
* -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920 2010-11-22 19:56:44.789 bCab[23049:207] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 4 节中的行数无效。包含的行数在更新后的现有节中 (1) 必须等于更新前该节中包含的行数 (0),加上或减去从该节插入或删除的行数(0 插入,0 删除)。 '
在数据源中,我根据第 3 节中的行数更新第 4 节。当从第 3 节中删除一行时,第 4 节中的行数从 0 变为 1。这似乎导致了问题。有没有办法避免这种情况?
任何指针将不胜感激。 谢谢。
更新:
numberOfSectionsInTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
返回 6;
}
numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
bCabAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if (section == 0) { // First name, last name
return 2;
}
else if (section == 1) { // Password
return 2;
}
else if (section == 2) { // Mobile, DOB , Gender
return 3;
}
else if (section == 3) { // credit cards
return [creditCards count];
}
else if (section == 4) { // Add credit card
if ([creditCards count] >= 3) {
return 0;
}
else {
return 1;
}
}
else if (section == 5) {
return 0;
}
return 0;
}
commitEditingStyle
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (indexPath.section == 3) {
// Delete the row from the data source
NSLog(@"%d %d", indexPath.section, indexPath.row);
[creditCards removeObjectAtIndex:indexPath.row];
// Delete from backend
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
//[tableView reloadData];
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
我尝试过使用和不使用 [tableView reloadData] 的结果相同。
感谢您的帮助!
【问题讨论】:
-
你能发布你的 numberOfSectionsInTableView 和 tableView:numberOfRowsInSection 方法吗?
-
你能发布删除行的代码吗?
标签: iphone objective-c uitableview