Bur*_*rak 3 uitableview delete-row
我在从表视图中删除行时遇到问题.当按下行中的删除按钮时,我正在使用下面的代码:
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];
Run Code Online (Sandbox Code Playgroud)
第一行已成功删除,但随后索引不正确.因此,当我删除最后一行时,它会给出索引超出范围的异常.
细胞生成代码是:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
更新:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
解决了我的问题.感谢Nenad M.
Nen*_*d M 11
假设您[NSIndexPath indexPathForRow:control.tag-100 inSection:0];返回正确的索引路径....您是否尝试removeObjectAtIndex在begin-/end-updates"括号"内移动调用?:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
[resultView beginUpdates];
[resultList removeObjectAtIndex:indexPath.row];
[resultView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[resultView endUpdates];
Run Code Online (Sandbox Code Playgroud)
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]]];
}
}
Run Code Online (Sandbox Code Playgroud)
现在做:
for (PersonalizeCell* cell in cells)
{
cell.rateView.tag = // new calculated tag
}
Run Code Online (Sandbox Code Playgroud)
(或者直接在第一个代码片段的内部循环中进行重新分配.)
这是整个过程的一些非常典型的代码,示例中的表中有两行:
请注意,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;
}
Run Code Online (Sandbox Code Playgroud)
注意:您的numberOfRowsInSection代码必须使用facebookRowsExpanded
(它会像"如果facebookRowsExpanded返回7,否则返回5")
注意:您的cellForRowAtIndexPath代码必须使用facebookRowsExpanded.
(它必须返回正确的行,具体取决于您是否展开.)
| 归档时间: |
|
| 查看次数: |
12464 次 |
| 最近记录: |