【发布时间】:2011-09-04 19:25:53
【问题描述】:
我正在尝试掌握 UITableViews 以及与之相关的所有内容。目前我有以下代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
-(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];
}
[cell.textLabel setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];
return cell;
}
- (IBAction)killItem {
NSIndexPath *indexToDelete = [NSIndexPath indexPathForRow:2 inSection:0];
[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexToDelete] withRowAnimation:UITableViewRowAnimationRight];
}
我在启动“killItem”功能时收到以下错误:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后现有节中包含的行数(10)必须等于行数更新前包含在该节中 (10),加上或减去从该节插入或删除的行数(0 插入,1 删除)。'
我理解它的方式 tableViews 基本上有一个委托和一个数据源,其中数据源决定了 tableView 中应该有多少行。通过在 stackoverflow 上的一些搜索,我发现当“数据源与现实不匹配”时,当它正在搜索我已删除的不存在的行时,会导致此错误。
我可能弄错了,但这就是我认为的做法。所以我的问题是,我如何让这些匹配,这样我就可以避免这个错误?
作为参考,我在不了解我需要做什么的情况下查看了以下帖子:
Error : Number of Rows In Section in UITableView in iPhone SDK
在 killItem 函数中添加[tbl reloadData]、[tbl beginUpdate] ... [tbl endUpdate] 似乎并没有解决我的问题。
提前谢谢你, 托拜厄斯·托维达尔
【问题讨论】:
标签: iphone objective-c ios uitableview