插入行后重新加载tableView

时间:2018-01-26 06:42:20

标签: ios swift uitableview

我有一个节标题,显示了评论的数量。当用户添加新评论时,我调用insertRow并想要更改显示评论数量的单元格。问题是,.reloadData的任何变体都会破坏动画。我已尝试使用CA.commit()在CAtransaction上设置完成并仅重新加载单个单元格。在 插入和滚动完成后,我可以重新加载数据的任何想法?

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

2 个答案:

答案 0 :(得分:0)

尝试以下代码(而不是重新加载整个表,只重新加载包含注释的单个行):

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)

此外,从代码中看不到它,但不要忘记更新数据,以便插入的行在数据模型中有一个支持新项目。

答案 1 :(得分:-1)

DispatchQueue.main.async{
       tableView.reloadData()
 }
相关问题