How can I calculate height of a custom UITableViewCell
by using UILabel
's content in table view cell?
Asked
Active
Viewed 448 times
0

Salman Zaidi
- 9,342
- 12
- 44
- 61

Arun
- 85
- 1
- 1
- 8
-
How many cells do you have in your table ? – n00bProgrammer Apr 21 '14 at 05:22
-
Ten custom cells and number of rows are dynamic – Arun Apr 21 '14 at 06:35
1 Answers
3
Implement your heightForRowAtIndexPath
like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = [dataArray objectAtIndex:indexPath.row]; //your data string
CGSize constraint = CGSizeMake(yourLabel.frame.size.width, 2000.0f);
CGSize size;
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [text boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:yourLabel.font}
context:context].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
return size.height;
}

Salman Zaidi
- 9,342
- 12
- 44
- 61