how to get a UITableView row height to auto-size to the size of the UITableViewCell?

前端 未结 5 841
梦谈多话
梦谈多话 2020-12-23 17:24

How to get a UITableView row height to auto-size to the size of the UITableViewCell?

So assuming I\'m creating the UITableViewCell in Interface Builder, and it hei

5条回答
  •  星月不相逢
    2020-12-23 18:06

    In the xib with your tableview, you can add a cell object and link it to an IBOutlet in your source-code. You won't use it anywhere, you will just use this to get the height of the cell.

    Then, in tableView:heightForRowAtIndexPath: you can use that object to get the height. It's not 100% automatic, but at least this saves you the trouble to manually update your source code when you make changes in the cell view in IB.

    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return myDummyCellObject.bounds.size.height;
    }
    

    If all rows are of the same type (cell) you can programatically set the tableView.rowHeight property instead of implementing the delegate method above. Depends on your scenario.

    Oh, and make sure you don't forget to release myDummyCellObject in -dealloc.

提交回复
热议问题