AppsDev
AppsDev

Reputation: 12509

How to avoid flicker when reloading expandable table view cells?

I need the cells of a UITableView to be expanded, but I see a flicker when doing this.

I have this implementation:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // Switch row state
    expandedFlags[indexPath.row] = !expandedFlags[indexPath.row]

    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { () -> Void in
            tableView.beginUpdates()
            tableView.reloadRows(at: [IndexPath(row: indexPath.row, section: 0)], with: UITableViewRowAnimation.automatic)
            tableView.endUpdates()
        }, completion: nil)
}

Is there any way to avoid the flicking when reloading the cells? I've read several posts but didn't work for my scenario.

Upvotes: 0

Views: 1715

Answers (3)

Prit Tejani
Prit Tejani

Reputation: 1

UITableView.performWithoutAnimation {
self.servicesTableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .none)
}

Upvotes: 0

Mandeep Singh
Mandeep Singh

Reputation: 2855

Hi After spending so many hours on this, I finally found the solution to stop this flicker issue in table view while you are expanding or collapsing your tableview cell.

In First step, you have to get your current offset of tableview and then stop the uiview animation and tell your tableview that you are going to update some rows in the tableview. Then pass your current index [IndexPath(row: 0, section: 3)] like this is mine. Your tableview works like charm.

let currentOffset = self.tableView.contentOffset
UIView.setAnimationsEnabled(false)
self.tableView.beginUpdates()
self.tableView.reloadRows(at: [IndexPath(row: 0, section: 3)], with: .none)
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
self.tableView.setContentOffset(currentOffset, animated: false)

Upvotes: 0

Marat Ibragimov
Marat Ibragimov

Reputation: 1084

tableView.beginUpdates()
....
tableView.endUpdates()

already has its own animation UIView animation what probably makes the flicker

Upvotes: 0

Related Questions