ios - 如何根据 UITextView 的内容调整 UITextView 和 UITableViewCell 的大小

标签 ios objective-c twitter uitableview

我见过一些类似的问题,但对其他人有用的其他解决方案都不适用于我,所以请不要将其标记为重复。我已经尝试了三天的替代解决方案,但没有成功。

作为应用程序的一部分,我的承包商要求我有一个选项卡,允许用户阅读带有特定主题标签的推文。我从 Twitter 中提取所有数据都很好,当我使用通用单元格(即高度 200,UITextView 高度 180)时,所有数据都进入并显示正常。但是,这显然看起来不太专业,我想将其更改为动态调整单元格的高度。

Interface Builder 中的 UI 如下所示(蓝色突出显示的是 UITextView):Cell's UI

谁能告诉我如何调整单元格和 TextView 的大小以正确显示内容?在这一点上我很迷茫。

最佳答案

你可以尝试使用this method .它与您的问题 99% 相似。先尝试让它只对Label起作用,然后再添加其他UIView

这里是关于问题解决的一些想法。

我将从上面的链接中复制部分代码。

首先,我将讨论只有一个标签的简化表格单元格。在 Storyboard中添加 4 个约束(尾部和前导、标签和 super View 之间的顶部和底部间距)。

在这种情况下,单元格的高度应如下所示:

Content view height = Top constraint + label height + Bottom constraint

- (void)configureCell:(CustomCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.customLabel.text = self.tableData[indexPath.row];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [self.tableView dequeueReusableCellWithReuseIdentifier:@"custom_cell"]
    [self configureCell:cell atIndexPath:indexPath];
    // here is HUGE mistake in link above! Be careful!
    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return height + 1;
}

这是代码的作用:

1. Configuring the content of the cell
2. Forcing a layout of the cell to apply constraints
3. Getting the height of the contentView, computed using Auto-Layout. We can’t directly call systemLayoutSizeFittingSize:UILayoutFittingCompressedSize: on the cell because the constraints we’ve set up are relative to the content view. Finally, we use UILayoutFittingCompressedSize to get the smallest size fitting the content.
4. Adding a bonus 1. We’ve computed the content view height but we actually need to return… the cell height here. And it’s 1 pixel higher, because of the separator, which height is 1 pt (0.5 for Retina screens on iOS 7, to be exact).

关于这个解决方案的讨论 此解决方案涉及更多计算,然后询问 [NSString -boundingRectWithSize:options:attributes:]。但在我看来,后一种方法会让您将所有设计(约束、字体设置等)直接转移到代码中,因为它只为您提供标签高度,而不是整个单元格。

假设您从顶部和底部缩进 10pt。对于新的更新,您决定将其更改为 5。您必须更改代码,否则它不会反射(reflect)更改。

当然,计算每个单元格的高度涉及更多的计算。幸运的是,在 iOs7 中引入了新的 UITableView 委托(delegate)方法:tableView:estimatedHeightForRowAtIndexPath:。大大减少了计算量,提高了性能。

我确实相信,您应该使用这篇文章中描述的方法(尽管事实上,iOs6 可能存在一些性能问题。我认为现在只有 5-10% 的 iOs6 用户并且还在减少。)

关于ios - 如何根据 UITextView 的内容调整 UITextView 和 UITableViewCell 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22054418/

相关文章:

ios - SWRevealViewController - 如何通过在 Front ViewController 上点击和/或滑动使菜单消失

ios - 我的 iOS 应用程序分辨率较低(包括状态栏)

IOS:如何在 UIMapView 上显示标签文本值

javascript - 使用 jQuery 和 Ajax 的 Twitter 搜索 API - 当我的演示未显示结果时如何检索数据

iphone - Twitter 是否可以在 iOS 后台自动分享?

android - 从 Twitter 结构中排除 crashlytics

ios - 如何在 iOS 上的 Protractor 测试中发送 ENTER 键?

ios - Cordova 在加载页面时卡住,Cordova 4.2.1,iOS 10.0

iphone - 如何从 objective-c 在 iPhone 上启动 map 应用程序

iphone - 为什么 NSDateFormatter 无法解析 ISO 8601 格式的日期