views:

56

answers:

2

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
}
+1  A: 

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.

Robot K
A: 

Do you set number of lines on the label? Because that's going to give you an answer that is the default, not the number of lines that match the text you are using...

I'd say the most likely cause of a crash is something to do with the textLabel. Try setting NSZombieEnabled to YES in the environment variables for the executable, and the log will tell you what kind of object is being called after it is released...

Kendall Helmstetter Gelner