Thursday, 24 January 2019

How to allow an off screen UITableViewCell push down content offset?

I have a UITableView, which contains a bunch of cells.

If a user clicks on the second cell, the first cell is supposed to animate and expand its height significantly and push all of the other cells down, leaving the user's scroll position in the same place. My code is working 100% correctly when both cells are on the screen. The contentSize of the UITableView grows significantly, and the contentOffset does not change.

But if the user scrolls down so that only the second cell is visible, when they tap it, the first cell expands off screen and nothing happens for the user.

The contentSize of the UITableView does not change, nor does the contentOffset. Once the user scrolls up slightly, and sees the bottom of the expanded cell, the contentSize and the contentOffset update to reflect the fact that the first cell is indeed much bigger (but nothing changes from the user's perspective)

Calling heightForRowAtIndexPath for the the index path of the cell before and after expanding returns the expected values.

My code has a lot going on, but the main piece that is supposed to be animating the expansion is here:

UIView animateWithDuration:0.3
 animations:^{
   performSelectorOnMainThread(@selector(updateItemHeights:), withObject: nil, waitUntilDone: YES)
 }
 completion:^(BOOL finished){}]

And the implementation of of updateItemHeights:

   beginUpdates
   endUpdates
   self.contentSize = sizeThatFits([contentSize.width, CGFLOAT_MAX])

It seems like iOS is trying to keep the user in their current context by allowing the cells above to expand.

How do I get an off screen cell to push the other cells down?



from How to allow an off screen UITableViewCell push down content offset?

No comments:

Post a Comment