如何使用动画从UITableView删除行? [英] How to delete rows from UITableView with animation?
问题描述
我在从表视图中删除行时遇到问题.当按下该行中的删除按钮时,我正在使用以下代码:
I am having problems with deleting rows from table view. I am using the code below when the delete button in the row was pressed:
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];
第一行已成功删除,但随后索引不正确.因此,当我删除最后一行时,它会给出索引超出范围的异常.
First row was deleted successfully but then, indexes were not correct. So when I delete the last row, it gives index out of bounds exception.
单元格生成代码为:
- (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.
solved my problem. Thanks to Nenad M.
推荐答案
假设您的 [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
返回正确的索引路径....您是否尝试过将 removeObjectAtIndex
调用移至开始/结束更新括号"内?:
Assuming your [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
returns the right index path....
Did you try moving the removeObjectAtIndex
call inside the begin-/end-updates "bracket"?:
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,从而导致超出范围的错误!
UPDATE:
Obviously your cell.rateView.tag
become wrong, after youe delete your first cell.
So after every deletion (i.e. every removeObjectAtIndex...
) you must re-iterate over your remaining tableview-cells an re-assign the proper tag-value (cell.rateView.tag = indexPath.row + 100
)! Otherwise your [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
will return a wrong indexPath, therefore leading to your out of bounds error!
您不必重新加载整个表,只需循环遍历其余单元格,然后在 [resultView endUpdates];
:
You don't have to reload the entire table, just loop through the remaining cells an re-assign the tag-value after [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
}
(或者甚至直接在第一个代码段的内部循环中进行重新分配.)
(Or do the re-assignment even in the inner-loop of the first code-snippet directly.)
下面是整个过程的一些非常典型的代码,示例中的表格两行:
Here's some really typical code for the whole process, two lines from the table in the example:
请注意,facebookRowsExpanded是您必须具有的类变量:
Note that facebookRowsExpanded is a class variable you must have:
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
NOTE: your code for numberOfRowsInSection must use facebookRowsExpanded
(类似于如果facebookRowsExpanded返回7,否则返回5")
(it will be something like "if facebookRowsExpanded return 7, else return 5")
注意: cellForRowAtIndexPath 的代码必须使用facebookRowsExpanded.
NOTE: your code for cellForRowAtIndexPath must use facebookRowsExpanded.
(它必须返回正确的行,具体取决于您是否展开.)
(it has to return the correct row, depending on whether or not you are expanded.)
这篇关于如何使用动画从UITableView删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!