我有一个节标题,显示了评论的数量。当用户添加新评论时,我调用insertRow并想要更改显示评论数量的单元格。问题是,.reloadData的任何变体都会破坏动画。我已尝试在CAtransaction上使用CA.commit()和完成集,并仅重新加载单个单元格。任何想法如何在插入和滚动完成后重新加载数据?
let indexPath:IndexPath = IndexPath(row:(self.comments.count - 1), section:3)
self.tableView.beginUpdates()
self.tableView.insertRows(at: [indexPath], with: .bottom)
self.tableView.endUpdates()
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
//I want to call self.tableView.reloadData() here
尝试以下代码(而不是重新加载整个表,只重新加载包含注释的单个行):
let indexPath:IndexPath = IndexPath(row:(self.comments.count - 1), section:3)
self.tableView.beginUpdates()
self.tableView.insertRows(at: [indexPath], with: .bottom)
// lets try to reload just the cell with comments
self.tableView.reloadRows(at: [indexPathOfCellWithComments], with: .fade)
self.tableView.endUpdates()
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
此外,从代码中看不到它,但不要忘记更新数据,以便插入的行在数据模型中有一个支持新项。
DispatchQueue.main.async{
tableView.reloadData()
}