Skip to content

Instantly share code, notes, and snippets.

@sumardi
Created October 23, 2011 08:01
Show Gist options
  • Save sumardi/1307027 to your computer and use it in GitHub Desktop.
Save sumardi/1307027 to your computer and use it in GitHub Desktop.
iPhoneDev - UITableViewCell dynamic height
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment