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)
http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ is a great tutorial for this.
The main bit is
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// Get the text so we can measure it
NSString *text = [items objectAtIndex:[indexPath row]];
// Get a CGSize for the width and, effectively, unlimited height
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
// Get the size of the text given the CGSize we just made as a constraint
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
// Get the height of our measurement, with a minimum of 44 (standard cell size)
CGFloat height = MAX(size.height, 44.0f);
// return the height, with a bit of extra padding in
return height + (CELL_CONTENT_MARGIN * 2);
}