0

I need to set UITableViewCell height based on my UITextView height, but when I access cell sublcass using: CommentTableViewCell *cell = (CommentTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; in heightForRowAtIndexPath:, app crashes.

What's wrong? Isn't this the right way to access my custom cell class?

user1118321
  • 25,567
  • 4
  • 55
  • 86
Armands L.
  • 1,885
  • 2
  • 14
  • 18
  • Getting the cell depends on the height and what you are doing is making the height depend on the cell, which will recurse infinitely. You should create a single static cell that you can reuse for calculating heights. [See my answer here for how to make this work](http://stackoverflow.com/questions/22819690/dynamic-uitableviewcell-height-with-variable-number-of-subviews/22820276#22820276). – Dima Apr 05 '14 at 01:02
  • Another [similar link](http://stackoverflow.com/questions/13660004/retrieve-custom-prototype-cell-height-from-storyboard/14127936#14127936) – Timothy Moose Apr 05 '14 at 01:53
  • heightForRowAtIndexPath call before cells are created,use class method for Calculate height in custom cell. – NANNAV Apr 05 '14 at 05:12

2 Answers2

0

If the UITextView height depends on some string length unknown until run time, then you should follow MVC architecture and determine the height from that string as defined in some model object. You can use NSString sizeWithFont or sizeWithAttributes methods to get the text size.

ax123man
  • 578
  • 5
  • 17
  • I had problems with these methods. sizeWithFont is deprecated since iOS 6.0 and sizeWithAttributes doesn't respect Word Wrap. – Armands L. Apr 05 '14 at 18:43
0

From my experience, heightForRowAtIndexPath is triggered automatically before it displays the cell. So you need to know the height of the cell before it is displayed. You can not do this in heightForRowAtIndexPath at is seems that you think that you can change it be looking at cell's text's height during heightForRowAtIndexPath.

What you need to do is create an array and store the height. You can now then use that array and extract the number inside heightForRowAtIndexPath.

Here's an example:

- (void) calculateHeight {
UILabel *tempTitle = [[UILabel alloc] init];
tempTitle.font = [UIFont fontWithName:@"your font" size:@"size"];
tempTitle.lineBreakMode = NSLineBreakByWordWrapping;


for (int i = 0; i < [yourListOfData count]; i++) {

    NSString *yourText = [yourListOfData objectAtIndex:i];

    CGSize textSize = [yourText
                   sizeWithFont:@"size"
                   constrainedToSize:CGSizeMake(0, "maxsize of your label that you want")
                   lineBreakMode:NSLineBreakByWordWrapping];


    [listOfHeightPerItem addObject:[NSNumber numberWithFloat:(textSize.height + 20)]];
    //+20 for padding
    }
}

and then in your heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat tempHeight = [(NSNumber *)[listOfHeightPerItem objectAtIndex:indexPath.row] floatValue];
    return tempHeight;
}

Please note that this isn't the most optimal way but it works... especially when your deadline was yesterday ;)

Francis Zabala
  • 1,037
  • 2
  • 11
  • 30