Reputation: 1095
guys. I have UITableView with different cells and I have code, which counts height. In one project it works perfect, but in second it returns height equals 0. What could be causing this? My code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
CGFloat cellWidth = 320.0f;
CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cellWidth, CGFLOAT_MAX) lineBreakMode:cell.textLabel.lineBreakMode];
CGFloat height = size.height;
NSLog(@"Height: %f", height);
return height;
}
Upvotes: 0
Views: 129
Reputation: 15722
The heightForRowAtIndexPath:
delegate method gets called before the cellForRowAtIndexPath:
delegate method. In your code you are calculating your cell height based on cell.textLabel.text, but the text in the cell has not been set yet.
You need to get the text to use in this method from somewhere else other than the table cell (presumably you can get it from wherever you are getting it from when you set the textlabel.text value in cellForRowAtIndexPath).
Upvotes: 3