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?