【发布时间】:2013-10-11 13:24:35
【问题描述】:
我通过 Interface Builder 向 tableView 添加了一些 tableViewCells,其中一个包含 UITextView。
当按钮被点击时,tableView 被重新加载:
- (void)updateViewWithMessage:(NSString *)message
{
self.someMessage = message;
[self.tableView reloadData];
}
在我的 cellForRowAtIndexPath 方法中,我检查此单元格并执行以下操作:
- (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;
}
在 heightForContentView() 我计算高度:
- (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;
}
如果我将行高度设置为某个值,则单元格会正确调整大小。但是里面的textView没有。 如果我单击按钮两次(再次调用 updateViewWithMessage: 方法)然后它会调整大小 - 这很奇怪!
顺便说一句:NSLog 打印的大小是正确的,所以肯定是设置好了,但是为什么这个 TextView 没有调整大小呢?
谁能帮我快速解决这个问题?
【问题讨论】:
标签: uitableview interface-builder ios7 uitextview