UITableView 未正确对在屏幕外重新排序的单元格进行动画处理



我正在使用NSFetchedResultsController作为我的数据源。

当我对一个单元格重新排序时,它向上或向下移动到屏幕上的位置或稍微离开屏幕的位置时,单元格会移动到带有动画的新位置。

但是,当行移动到远离屏幕的新位置时,它只是在没有任何动画的情况下移动。

理想情况下,我希望这些情况下的行向下或向上动画,直到它离开屏幕。 有没有办法在不实现自定义方法的情况下实现这一点?

我在这里使用的情况是以下委托调用中的 .move:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
tableView.insertRows(at: [newIndexPath!], with: UITableViewRowAnimation.none)
case .delete:
tableView.deleteRows(at: [indexPath!], with: UITableViewRowAnimation.none)
case .update:
tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)
case .move:
tableView.moveRow(at: indexPath!, to: newIndexPath!)
}
}

从文档中,UIKit将为所有单元格的移动操作设置动画,但是它发生得如此之快,以至于它不是很直观。

因此,您实际上可以通过两次move(at:,to:)调用获得所需的效果,performBatchUpdates如下所示:

guard indexPath != newIndexPath, let paths = tableView.indexPathsForVisibleRows else { return }
if paths.contains(newIndexPath!) {
tableView.moveRow(at: indexPath!, to: newIndexPath!)
} else {
tableView.performBatchUpdates({
let index = indexPath < newIndexPath ? (paths.count - 1) : 2
tableView.moveRow(at:indexPath!, to: paths[index])
tableView.moveRow(at: paths[index], to: newIndexPath!)
})
}

请注意,要实现向上滚动动画,您必须将paths索引设置为 2 作为向上移动的中点。

最新更新