I'd like to make a custom UITableViewCell height using (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
.
I'm trying to get the height of the UITableViewCell
by multiplying the number of lines in textLabel
by the height of each line and then adding 10.0f
for the subtitle. I am using the following code and getting exc_bad_access()
. Why?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ([[[tableView cellForRowAtIndexPath:indexPath] textLabel] numberOfLines] * [[[[tableView cellForRowAtIndexPath:indexPath] textLabel] font] lineHeight]) + 10.0;
//return kRowHeightiPod; //A constant value for a sanity check
}
Before you can get the cell, you need to compute it's height. You've just made computing it's height dependent on getting the cell. You've created an infinite recursion.
You're going to need to find another way to do your calculation, probably by examining the data you're adding to the table cell, rather than querying the table cell directly.
Also, note that numberOfLines
represents the number of lines a text label is capable of displaying, but it may display less if there's not enough content. In other words, even if it's only displaying one line of text, numberOfLines
will return two if that's the maximum number of lines the text label is capable of displaying.
What you probably want is to use one of the NSString UIKit additions to compute the height of text text you want to display in the cell.