Art*_*iom 1 xcode objective-c uitableview ios swift
在我的中tableView,我删除了一行,之后我需要重新加载整个表以重新计算一些值。最好的方法是什么?由于deleteRows没有完成处理程序,并且根据对 SO 的评论,我永远不应该tableView.reloadData()在删除行后立即调用。
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "Delete") { [weak self] (action, view, completion) in
guard let weakSelf = self else { return }
CoreDataManager.manager.deleteActivity(activity: weakSelf.sortedActivites[indexPath.row])
weakSelf.sortedActivites.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.reloadData()
completion(true)
}
deleteAction.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [deleteAction])
}
Run Code Online (Sandbox Code Playgroud)
对于你的情况,只需要打电话reloadData就足够了。
如果您需要 的完成回调deleteRows,您可能需要尝试在块内deleteRows使用none动画进行调用UIView.animate。
UIView.animate(
withDuration: 0.2,
animations: {
self.tableView.deleteRows(at: indexPaths, with: .none)
},
completion: { _ in
self.tableView.reloadData()
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
704 次 |
| 最近记录: |