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

后端 未结 12 2066
青春惊慌失措
青春惊慌失措 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:28

    Your problem is in setExpanded: in JVCell.m, you are directly editing the frame of the target cell in that method.

    - (void)setExpanded:(BOOL)expanded
    {
        expanded_ = expanded;
    
        CGFloat newHeight = heightCollapsed_;
        if( expanded_ ) newHeight = heightExpanded_;
    
        CGRect frame = self.frame;
        frame.size.height = newHeight;
        self.frame = frame;
    }
    

    Update it to:

    - (void)setExpanded:(BOOL)expanded
    {
        expanded_ = expanded;
    }
    

    Then remove the call to -reloadRowsAtIndexPaths:withRowAnimation: at line 163 of JVViewController.m and it will animate as expected.

    -reloadRowsAtIndexPaths:withRowAnimation: expects different cells to be returned for the provided indexPaths. Since you are only adjusting sizes -beginUpdates & -endUpdates is sufficient to layout the table view cells again.

提交回复
热议问题