在UITableViewCell中调整UITextView的大小时会发生奇怪的事情

时间:2012-11-26 09:15:19

标签: uitextview autoresize

背景

我有一个Master-Detail视图拆分视图控制器。在详细的表视图中,我有一个静态表视图,其中一个单元格只包含一个UITextView来显示客户的注释。

当我在主视图中点击客户名称时,它会读取注释信息并在详细视图的viewWillAppear中的UITextView中的文本中进行设置。同时,UITextView使用以下代码调整大小:

CGRect frame;
frame = self.detailCustomerCommentUITextView.frame;
frame.size.height = [self.detailCustomerCommentUITextView contentSize].height;

self.detailCustomerCommentUITextView.frame = frame;
[self.tableView reloadData];

此单元格的高度也发生了变化:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 30.0;

if (indexPath.section==3&&indexPath.row==1)
{
    height = MAX(self.detailCustomerCommentUITextView.frame.size.height, 30.0);

}else if (indexPath.section == 0 && indexPath.row == 0)
{
    height = 55.0;
}else if (indexPath.section == 1 && indexPath.row == 0)
{
    height = 47.0;
}

    return height;
}

重现问题的步骤:

我第一次点击客户(客户简称),其中包含简短的评论文本。调整大小的框架是: (CGRect)frame = origin =(x = 67,y = -6)size =(width = 568,height = 32)

我在断点后得到了这个值:     self.detailCustomerCommentUITextView.frame = frame;

然后在heightForRowAtIndexPath中,我得到了相同的高度:

(CGFloat)身高= 32

这看起来不错,但在应用程序中我没有在UITextView中看到任何评论!

我很确定这个文本视图的文本不是空的,因为我已经在调试模式下检查了它。

然后我再次点击同一个客户,在断点后:

frame = self.detailCustomerCommentUITextView.frame;
我看到了 (CGRect)frame = origin =(x = 67,y = -6)size =(width = 568,height = 10)

这意味着在最后一帧调整到高度“32”后,UITextView的大小调整为“10”。

然后代码贯穿

frame.size.height = [self.detailCustomerCommentUITextView contentSize].height;

self.detailCustomerCommentUITextView.frame = frame;

我看到身高再次被设定为32。但这次一切都很完美。我看到评论文本和UITextView已调整大小:

然后我重复上述步骤,看到一切正常,UITextView不再调整大小。

问题摘要:

在我第一次点击客户Short时,为什么UITextView在成功调整为高度32后调整为高度10?

我使用单步调试,但找不到任何其他更改框架的代码。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

问题已解决。我发现虽然调整了UITextView的大小,当它调用heightForRowAtIndexPath时,UITextView的框架与新的单元格高度重叠。

在故事板中,UITextView - >尺寸检验员 - >查看 - >自动调整,我删除所有内部箭头,只留在我外面(抱歉,我不知道哪个是Struts,哪个是Springs)。通过这种方式,UITextView将自动调整大小以适应单元格的新高度。

希望这有助于满足任何人的需求。

相关问题