Home > database >  Animation in UITableViewCell breaks after cell deallocation
Animation in UITableViewCell breaks after cell deallocation

Time:08-15

I have an expanding view in the cell. After i press show more button it works fine. But when i scroll down and that cell deallocates from memory and then i tap on show more button again animation breaks. How can i fix it?

Project repo on Github

Collapsed view: Collapsed view

Expanded view: Expanded view

Debugger view, before scroll: Debugger view, before scroll

After cell deallocation (after scroll) breaks like this, expands behind second cell: After cell deallocation (after scroll) breaks like this, expands behind second cell

CodePudding user response:

Based on your code and comment...

Instead of expanding your cell, you're setting clipsToBounds = false and expanding the cell's contents, allowing them to extend outside the bounds of the cell.

As you scroll a UITableView, cells are - of course - added / removed as needed. But when they are re-added, the "z-order" changes (because, with "standard" table view usage, it doesn't matter).

So, after scrolling down and then back up, and then tapping the "Show more" button, that cell may be (and usually is) at a lower z-order ... and thus its expanded "out-of-bounds" content is hidden behind the cell(s) below it.

If you want to stick with that approach (as opposed to expanding the cell itself), you can try implementing scrollViewDidScroll to make sure that cell is "on the top":

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
    // make sure the first row is visible (not scrolled off the top of the view)
    let pth = IndexPath(row: 0, section: 0)
    if let a = tableView.indexPathsForVisibleRows,
       a.contains(pth),
       let c = tableView.cellForRow(at: pth) {
        // bring it to the front -- i.e. the top of the z-order
        tableView.bringSubviewToFront(c)
    }
    
}
  • Related