Bou*_*Bou 9 objective-c uitableview
当用户想删除一个单元格时,我创建了一个更新我的UITableView的方法.
-(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];
}
Run Code Online (Sandbox Code Playgroud)
p_currentFriends是一个NSMutableArray,它包含UITableView打印的所有对象.
当我使用它一次时,此方法可以正常工作,但是当我多次使用它时它会失败.
似乎每次调用deleteRowsAtIndexPaths时indexPath都保持不变.
例如, - 在第一次调用时,用户点击第二个单元格,indexPath = [0,1]并删除正确的单元格 - 在第二次调用时,用户点击第三个单元格并且indexPath = [0, 3]而不是[0,2]
/*希望我足够清楚:/*/
我发现使其工作的唯一方法是使用reloadData,但我希望在删除单元格时有动画.
有没有人有解决方案或提示?
你正在做的事情基本上是正确的,但你需要以tableView某种方式重新加载.如果你不想重新加载整个tableView,请使用它,这也给你一个animation-
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionNum];
[self.o_table reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
Run Code Online (Sandbox Code Playgroud)
斯威夫特2.2
我发现最好的解决方案是设置一个,NSTimer并在0.5秒后调用重载数据,这为动画制作提供了足够的时间。Selector("loadData")是一个包含以下内容的方法 tableview.reloadData()
_ = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("loadData"), userInfo: nil, repeats: false)
Run Code Online (Sandbox Code Playgroud)
斯威夫特3.0
_ = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(ViewController.loadData), userInfo: nil, repeats: false)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3087 次 |
| 最近记录: |