【发布时间】:2017-04-20 16:58:21
【问题描述】:
我有兴趣使用动画tableView.deleteRows(at: [singleIndexPath], with: .automatic) 从UITableView 中删除一批行。然而,我的目标是级联删除动画,以便它们以级联动画的方式发生,这样,动画将显示它们一个接一个地被删除,而不是整批被删除。
我发现了一个有用的 UITableView 扩展,在动画完成后我会得到一个完成块,例如:
extension UITableView {
/// Perform a series of method calls that insert, delete, or select rows and sections of the table view.
/// This is equivalent to a beginUpdates() / endUpdates() sequence,
/// with a completion closure when the animation is finished.
/// Parameter update: the update operation to perform on the tableView.
/// Parameter completion: the completion closure to be executed when the animation is completed.
func performUpdate(_ update: ()->Void, completion: (()->Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
// Table View update on row / section
beginUpdates()
update()
endUpdates()
CATransaction.commit()
}
}
我的问题是弄清楚如何利用块自动化行删除(以及后来的行插入)的动画。现在,下面两行 IndexPaths 的示例代码仍然同时出现动画,这不是我的目标。
let singleIndexPath = IndexPath(row: 0, section: 0)
let anotherIndexPath = IndexPath(row: 1, section: 0)
self.tableView.performUpdate({
self.tableView.deleteRows(at: [singleIndexPath, anotherIndexPath], with: .automatic)
self.tableView.insertRows(at: [singleIndexPath, anotherIndexPath], with: .left)
//Happening at the same time, which is not what I want
}, completion: {
})
非常感谢任何提示。
【问题讨论】:
-
您是否必须将要删除的每个索引路径放入单独调用
performUpdate的更新处理程序中? -
@paulvs 是的,但这不会自动进行,也不会基于我拥有的行数。我可以对行进行硬编码,但它是无效的。
标签: ios swift uitableview uiviewanimation