1

I added some tableViewCells to a tableView via Interface Builder, one of them is containing a UITextView.

The tableView is reloaded as an Button gets clicked:

- (void)updateViewWithMessage:(NSString *)message
{
    self.someMessage = message;
    [self.tableView reloadData];
}

In my cellForRowAtIndexPath method I check for this cell and do the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellIdentifier = kMyCustomCell;
    MyCustomCell *contentCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    CGRect textViewFrame = contentCell.contentTextView.frame;
    textViewFrame.size.height = [self heightForContentView];

    NSLog(@"%f, %f, %f, %f\n", textViewFrame.origin.x,
          textViewFrame.origin.y,
          textViewFrame.size.width,
          textViewFrame.size.height);

    contentCell.contentTextView.frame = textViewFrame;
    contentCell.contentTextView.text = self.someMessage;

    return contentCell;
}

In heightForContentView() I calculate the height:

- (CGFloat)heightForContentView
{
    CGSize boundingRectSize = CGSizeMake(kCustomCellWidth - 20, CGFLOAT_MAX);
    CGRect textViewRect = [self.someMessage boundingRectWithSize:boundingRectSize
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:nil
                                                 context:nil];
    return textViewRect.size.height + 20;
}

The cell, if I set the row Height to something, resizes properly. But the textView in it does not. If I click my button twice (the updateViewWithMessage: method gets called again) and then it resizes - that's weird!

By the way: NSLog prints the correct sizes, so they are definitely set, but why does this TextView not resize then?

Can someone help me to solve this problem quickly please?

Kuno
  • 75
  • 1
  • 11

1 Answers1

1

EDIT after comments :

I think you don't resize your UITextView, add this code before to resize contentCell:

contentTextView.frame = CGRectMake(CGRectGetMinX(contentTextView.frame), CGRectGetMinY(contentTextView.frame), CGRectGetWidth(contentTextView.frame), textViewFrame.size.height);

Old Answer :

Maybe you forgot to pass attributes to calculate the new size :

CGRect textViewRect = [self.someMessage boundingRectWithSize:boundingRectSize
                                             options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                          attributes:@{NSFontAttributeName:FONT}
                                             context:nil];

With FONT is your current font.

Have a look here.

Community
  • 1
  • 1
Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
  • you cell resize correctly from the new size of your uitextview but your textview don't resize ? – Jordan Montel Oct 11 '13 at 13:36
  • Yes, I can resize the cell size as height as I want - OK. But I cannot change the size of its containing UITextView, it only changes if I change it in interface builder. But I want to have it more dynamically... The changes I do programatically on the TextView just have NO effect concerning its SIZE. – Kuno Oct 11 '13 at 13:42
  • try this options (NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) – Jordan Montel Oct 11 '13 at 14:04
  • nope, does not work as well. The assignment of a new frame to the TextView's property should do the job also. But what's to weird is - as soon as I call updateViewWithMessage: the textView does not get resized. If I call it a second time it does ... – Kuno Oct 11 '13 at 14:23
  • Hum I think your `[self.tableView reloadData];` doesn't work very well. – Jordan Montel Oct 11 '13 at 14:38
  • I guess there is no other option? – Kuno Oct 11 '13 at 14:45
  • I don't think so. Look if your tableView delegate and data source are correctly set, if your .h include and if your tableView outlet is correctly linked – Jordan Montel Oct 11 '13 at 15:00
  • Solved it via manually programming this crazy UITextView - [cell addSubview:textView]; – Kuno Oct 11 '13 at 15:27