【问题标题】:Weird thing happens when resizing UITextView in a UITableViewCell在 UITableViewCell 中调整 UITextView 的大小时会发生奇怪的事情
【发布时间】:2023-03-15 15:07:01
【问题描述】:

背景:

我有一个主从视图拆分视图控制器。在详细表视图中,我有一个静态表视图,其中一个单元格只包含一个 UITextView 来显示客户的评论。

当我在主视图中点击客户名称时,它会读取评论信息并将其设置在 UITextView 中详细视图的 viewWillAppear 中的文本中。同时,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;
}

重现问题的步骤:

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

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

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

(CGFloat) 高度 = 32

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

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

然后我再次点击同一客户,在 Break point after:

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?

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

非常感谢您的帮助。

【问题讨论】:

    标签: uitextview autoresize


    【解决方案1】:

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

    在情节提要中,UITextView -> Size Inspector -> View -> Autosizing,我删除了所有内部箭头,只留下外部 I(抱歉,我不知道哪个是 Struts,哪个是 Springs)。这样,UITextView 将自动调整大小以适应单元格的新高度。

    希望这对任何需要的人都有帮助。

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      相关资源
      最近更新 更多