问题描述
令人沮丧的事实:调用tableView:moveRowAtIndexPath:toIndexPath:
后,该行未调用tableView:cellForRowAtIndexPath:
.
Frustrating fact: After calling tableView:moveRowAtIndexPath:toIndexPath:
, tableView:cellForRowAtIndexPath:
doesn't get called for that row.
在UITableView
更新块内的tableView:moveRowAtIndexPath:toIndexPath:
之前或之后调用reloadRowsAtIndexPaths:withRowAnimation:
也不起作用:这会引发不一致错误.
Call reloadRowsAtIndexPaths:withRowAnimation:
after or before tableView:moveRowAtIndexPath:toIndexPath:
within UITableView
updates block also doesn't work: it raises Inconsistency error.
我看到2种解决方法:删除+插入而不是移动或在另一个更新块中重新加载.
I see 2 workarounds: delete+insert instead of move or do reload within another updates block.
我的问题是:还有其他方法可以在同一更新块内重新加载和移动UITableView的行吗?
My question is: is there some other way to reload and move UITableView's row within same updates block?
推荐答案
我认为不可能同时进行移动和重新加载.我已经尝试了几种方法,而我想出的最好的解决方案是在批处理更新之前进行重新加载.我不设置reloadRows
动画,因为它似乎与批量更新动画冲突.
I do not think it is possible to do a move and a reload at the same time. I've tried several approaches and the best solution I've come up with is to do the reload just before the batch updates. I don't animate reloadRows
because it seems to conflict with the batch update animations.
[tableView reloadRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationNone];
[tableView beginUpdates];
//inserts, deletes and moves here
[tableView endUpdates];
此外,我通常将单元配置逻辑放在单独的方法中,例如:
Also, I typically put my cell configuration logic in a separate method like:
- (void)tableView:(UITableView *)tableView configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
以便我可以直接调用它并完全绕过reloadRowsAtIndexPaths
.您也不会以这种方式获得内置动画,但是您可以制作自己的动画.
So that I can just call it directly and bypass reloadRowsAtIndexPaths
altogether. You won't get the built in animation this way either, but you can do your own animations.
这篇关于如何重新加载以编程方式移动的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!