问题描述
我有一个由NSFetchedResultsController支持的UITableView,它可以随时触发更新.如果用户当前正在对行进行重新排序,则应用这些更新将导致异常,因为表视图已被临时接管,并且您会收到类似
I have a UITableView backed by an NSFetchedResultsController which may trigger updates at any time. If the user is currently reordering rows, applying these updates will cause an exception because the table view has temporarily taken over and you get an error like
Invalid update: invalid number of rows in section [...]
如何检测用户何时开始移动单元格,从而可以延迟由获取的结果控制器引起的更新?似乎没有任何表视图委托方法可以检测到这一点.该委托方法:
How can I detect when the user has started moving a cell so I can delay updates caused by the fetched results controller? There don't seem to be any table view delegate methods to detect this. This delegate method:
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
仅当用户最初将第一个单元格移至其他位置时,才调用此操作.
Doesn't get called when the user initially detaches the first cell, only when they actually move it somewhere else.
一种解决方案是在获取的结果回调中检查isEditing
并仅执行批量reloadData
而不是动态插入/删除行,但是我想知道是否有一种方法可以专门检查重新排序"模式.
One solution is to check isEditing
in the fetched results callbacks and just do a bulk reloadData
rather than dynamically inserting/deleting rows, but I'm wondering if there is a way to check specifically for the 'reordering' mode.
推荐答案
您错了,tableView:canMoveRowAtIndexPath无法解决问题.
You are wrong, tableView:canMoveRowAtIndexPath does not solve the problem.
在要更新或删除的所有单元格上使用[cell setEditing:NO].
Use [cell setEditing:NO] on all cells you want to update or delete.
如果用户拖动单元格,则拖动模式将被取消.
If the user was dragging a cell, drag mode will be cancelled.
如果您需要[cell setEditing:YES],则可以在完成所有更新后立即将其还原.
If you need [cell setEditing:YES] you can restore it immediately after finishing whatever you are updating.
这篇关于检测何时对UITableView进行重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!