Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a custom UITableViewCell. In its function - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier, I need to know the cell height as defined in the tableView function heightForRowAtIndexPath so I can properly position UITextField, UIButton, etc in the cell. . Any ideas?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The way I usually go about doing this is I add a method to my NSObject subclass that will act as my datasource object (what goes into the datasource array, assuming you're using this basic approach).

eg. Say we need to display a bunch of blog posts (pure text), each post is a cell. Since each row will have variable height, I create an NSObject subclass, call it BlogPostInfo. In this class, I add the method:

- (int)cellHeight;
{
    /* Perform a calculation with blog data, probably using sizeWithFont: */
}

Since you have this method in your data object, you can use it as follows in the UITableViewController:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    /* assuming blogPosts is an NSMutableArray or whatevs */
    return [[blogPosts safeObjectAtIndex:indexPath.row] cellHeight];
}

That's how I do dynamic heights of tableviewcells.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.