I’m trying to return the height of the detailTextLabel in the heightForRowAtIndexPath delegate method. This way my table view cells should be the same height as my detailTextLabel.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *text = cell.detailTextLabel.text;
CGSize size = cell.detailTextLabel.bounds.size;
UIFont *font = cell.detailTextLabel.font;
CGSize labelSize = [text sizeWithFont:font
constrainedToSize:size
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height;
}
However I’m getting a EXC_BAD_ACCESS on the following line:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Wats wrong with the above piece of code?
The problem is that
heightForRowAtIndexPathis called beforecellForRowAtIndexPath. So the cell has not been created yet.If all rows have the same height you can specify a common height for the table. In your
viewDidLoadyou can just sayself.tableView.rowHeight = ABC;You can also set this in Interface builder.