简体   繁体   中英

reload tableView after inserting row

I have a section header which shows the number of comments. When the user adds a new comment, I call insertRow and want to change the cell that shows the number of comments. The problem is though that any variation of .reloadData ruins the animation. I've tried using CA.commit() with completion set on CAtransaction and reloading just the individual cell. Any ideas how I can reload the data after both the insertion and scrolling are complete?

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

Try following code (instead of reloading the whole table, reload just the single one row that contains comments):

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)

Moreover, it cannot be seen from the code, but don't forget to update the data so that inserted row has a backing new item in data model.

DispatchQueue.main.async{
       tableView.reloadData()
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM