1

I try to reloadData on UITableView at viewWillApper. But deselectRow Animation is not working well. How can I do reloadData & deselectRow animation?

override func viewWillAppear(_ animated: Bool) {

    self.tableView.reloadData()
    if let indexPathForSelectedRow = tableView.indexPathForSelectedRow {
            self.tableView.deselectRow(at: indexPathForSelectedRow, animated: true)
    }
    super.viewWillAppear(animated)
}

and below is different. Fade animation duration is little bit short.

override func viewWillAppear(_ animated: Bool) {

    if (self.tableView.indexPathForSelectedRow != nil){
        self.tableView.reloadRows(at: [self.tableView.indexPathForSelectedRow!], with: .fade)
    } 

    super.viewWillAppear(animated)
}
Tarou
  • 57
  • 7
  • 1
    I would advise not to reload data in this case at all. If you only want to update the contents of the cells, just iterate over `visibleCells` and update their contents directly. – Sulthan Mar 14 '18 at 15:40
  • Did you try just using `deselectRow(at:)` and no reload of any kind? – rmaddy Mar 14 '18 at 19:12
  • @rmaddy thank you for advice. In case using deselectRow(at:) and no reload is not to update the contents of the cell... – Tarou Mar 15 '18 at 13:51
  • @Sulthan thank you! The way you advice works. I show succeed code below code. – Tarou Mar 15 '18 at 14:12

2 Answers2

1

Instead of reloading the whole table, you can just update the contents of the cell directly:

override func viewWillAppear(_ animated: Bool) {
    let indices = self.tableView.indexPathsForVisibleRows ?? []
    for index in indices {
       guard let cell = self.tableView.cellForRow(index) else { continue }
       cell.textLabel?.text = textDataArray[index.row]
    }

    super.viewWillAppear(animated)
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

I can update cell with deselect rows animation.

override func viewWillAppear(_ animated: Bool) {
    var indexes = self.tableView.indexPathsForVisibleRows
        var i = 0
        for var records in self.tableView.visibleCells{
            records.textLabel?.text = textDataArray[indexes![i].row]
        i = i + 1
    }
}
Tarou
  • 57
  • 7