Why do my table view cells disappear when reloaded using reloadRowsAtIndexPaths?

后端 未结 12 1774
青春惊慌失措
青春惊慌失措 2021-02-02 12:20

I have a bare-bones sample project here:

http://dl.dropbox.com/u/7834263/ExpandingCells.zip

In this project, a UITableView has a custom UITableViewCell. In each

12条回答
  •  醉酒成梦
    2021-02-02 12:44

    The cell that is fading out is the previous cell that is not changing size. As the documentation of reloadRowsAtIndexPaths:withRowAnimation: states:

    The table animates that new cell in as it animates the old row out.

    What happens is the opacity is set to 1 then immediately set to 0 and so it fades out.

    If both the previous and the new cell change size then it works as intended. This is because the begin/end Updates notice the height changes and create new animations on those cells overriding the reloadRowsAtIndexPaths:withRowAnimation: ones.

    Your problem is due to abusing reloadRowsAtIndexPaths:withRowAnimation: to resize the cells when it's intended for loading new cells.

    But you don't need reloadRowsAtIndexPaths:withRowAnimation: at all. Just change the expanded state of the cells and do the begin/end updates. That will handle all the animation for you.

    As a side note I found the blue selection a little annoying, in JVCell set the selectedBackgroundView to the same image as the backgroundView (or create a new image that has the correct look of a selected cell).


    EDIT:

    Move the statement adding previousIndexPath_ to indicesToReload to the if statement (at line 132) so that it is only added if the previous cell was expanded and needs to resize.

    if( expanded ) {
        [previousCell setExpanded:NO];
        [indicesToReload addObject:[previousIndexPath_ copy]];
    }
    

    This removes the case where the previous collapsed cell would disappear.

    Another option would be to set previousIndexPath_ to nil when the current cell is collapsed and only set it when a cell expands.

    This still feels like a hack. Doing both the reloadRows and the begin/end Updates causes the tableView to reload everything twice but both seem to be needed to animate correctly. I suppose if the table is not too large this won't be a performance problem.

提交回复
热议问题