我在从表视图中删除行时遇到问题。当按下行中的删除按钮时,我正在使用下面的代码:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
[resultList removeObjectAtIndex:indexPath.row];
[resultView beginUpdates];
[resultView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[resultView endUpdates];
//[resultView reloadData];
第一行已成功删除但后来索引不正确。因此,当我删除最后一行时,它会给出索引超出范围的异常。
细胞生成代码是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"personalizeTableCell";
PersonalizeCell *cell = (PersonalizeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[PersonalizeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.title.text = @"text";
cell.rateView.tag = indexPath.row + 100;
return cell;
}
我哪里错了?
更新:
for (NSInteger j = 0; j < [venuesTableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [venuesTableView numberOfRowsInSection:j]; ++i)
{
PersonalizeCell* cell = (PersonalizeCell*)[venuesTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]];
cell.rateView.tag = 100 + i;
}
}
解决了我的问题。感谢Nenad M。
答案 0 :(得分:11)
假设您的[NSIndexPath indexPathForRow:control.tag-100 inSection:0];
返回正确的索引路径....
您是否尝试在开始/结束更新“括号”内移动removeObjectAtIndex
调用?:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
[resultView beginUpdates];
[resultList removeObjectAtIndex:indexPath.row];
[resultView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[resultView endUpdates];
显然,在您删除第一个单元格后,您的cell.rateView.tag
会出错。
因此,在每次删除后(即每removeObjectAtIndex...
),您必须重新迭代剩余的tableview-cells,重新分配正确的标记值(cell.rateView.tag = indexPath.row + 100
)!否则,您的[NSIndexPath indexPathForRow:control.tag-100 inSection:0];
将返回错误的indexPath,从而导致您的越界错误!
您不必重新加载整个表格,只需遍历剩余的单元格,然后在[resultView endUpdates];
之后重新分配标记值:
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
{
[cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}
现在做:
for (PersonalizeCell* cell in cells)
{
cell.rateView.tag = // new calculated tag
}
(或者直接在第一个代码片段的内部循环中进行重新分配。)
以下是整个过程的一些非常典型的代码,示例中的表格中有两行:
请注意,facebookRowsExpanded是您必须拥有的类变量:
if ( [theCommand isEqualToString:@"fbexpander"] )
{
NSLog(@"expander button......");
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray *deleteIndexPaths;
NSArray *insertIndexPaths;
facebookRowsExpanded = !facebookRowsExpanded;
// you must do that BEFORE, not AFTER the animation:
if ( !facebookRowsExpanded ) // ie, it was just true, is now false
{
deleteIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
nil];
[tableView beginUpdates];
[tableView
deleteRowsAtIndexPaths:deleteIndexPaths
withRowAnimation: UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
else
{
insertIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
nil];
[tableView beginUpdates];
[tableView
insertRowsAtIndexPaths:insertIndexPaths
withRowAnimation: UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
// DO NOT do this at the end: [_superTableView reloadData];
return;
}
注意: numberOfRowsInSection 的代码必须使用facebookRowsExpanded
(这将是“如果facebookRowsExpanded返回7,否则返回5”)
注意: cellForRowAtIndexPath 的代码必须使用facebookRowsExpanded。
(它必须返回正确的行,具体取决于您是否展开。)