Alex Rivera | Logout

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

Asked 2011-03-01T23:41:01.507
23

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 height is more than the standard size, how can I get the UITableView row height to autosize to it? (i.e. as opposed to manually having to measure the height in interface builder and than programmatically setting it)

Edit
Report

1 Answer

13

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.

answered 2011-03-02T00:08:37.747

Your Answer