使用 deleteRowsAtIndexPaths 后未更新 indexPath [英] indexPath not updated after using deleteRowsAtIndexPaths
问题描述
我创建了一个方法,当用户想要删除一个单元格时更新我的 UITableView.
I created a method which updates my UITableView when the user wants to delete a cell.
-(void)removeMoreFriendsRow:(NSNotification *)notification {
NSDictionary *d = [notification userInfo];
NSIndexPath *index = [d objectForKey:@"indexPath"];
[self.p_currentFriends removeObjectAtIndex:index.row];
[self.o_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationRight];
}
p_currentFriends 是一个 NSMutableArray,它包含 UITableView 打印的所有对象.
p_currentFriends is a NSMutableArray which contains all the objects printed by the UITableView.
这个方法用一次就可以,但是用多了就不行了.
This method works fine when I use it once, but it fails when I use it more than once.
每次调用 deleteRowsAtIndexPaths 时,indexPath 似乎都保持不变.
It appears that the indexPath stay the same at each call of deleteRowsAtIndexPaths.
例如
- 在第一次调用时,用户点击第二个单元格,indexPath = [0,1] 并删除正确的单元格,
- 在第二次调用时,用户点击第三个单元格并且 indexPath = [0,3] 而不是 [0,2]
我发现让它起作用的唯一方法是使用 reloadData,但我想在删除单元格时制作动画.
The only way I found to make it works is to use reloadData but I want to have a animation on the cell deletion.
我该怎么做?
推荐答案
您所做的基本上是正确的,但是您需要以某种方式重新加载 tableView
.如果您不想重新加载整个 tableView
,请使用它,它还会为您提供 animation
-
What you're doing is basically correct, but you'll need to reload the tableView
somehow. If you don't want to reload the whole tableView
, use this, which gives you an animation
as well -
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionNum];
[self.o_table reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
这篇关于使用 deleteRowsAtIndexPaths 后未更新 indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!