gpt4 book ai didi

ios - 如何使用自动布局在自定义 UITableViewCell 中动态设置 UITextView 高度

转载 作者:技术小花猫 更新时间:2023-10-29 11:21:58 24 4
gpt4 key购买 nike

我有 UITableView,每个 tableViewCell 都是自定义的。在我的 customTableViewCell 中是一个 UITextView,TextViews 框架是 pin 或与其 superView 相同,即 tableViewCell。 enter image description here

我如何动态设置与 TextViews 大小成比例的 UITableViewCell 高度,这也取决于我从 Internet 获得的内容文本。

(示例原型(prototype))

enter image description here

//这就是我操纵每个单元格高度的方式

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// How do I set the height here, whats the best approach for this. with autolayout BTW
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// I initialize every cell here with my ArrayContainer, nothing much to refer here.
}

最佳答案

  1. 按照这个答案:Link reference

  2. 向您的 UITextView 添加高度约束并为您的自定义单元格类创建导出。

  3. UITextView 上使用 sizeThatFits: 获取适合内容的大小,并将此值设置为约束的 常量 2 中描述。在 tableView:heightForRowAtIndexPath: 中执行此操作。

需要注意的是,如果有很多单元格(成百上千),那么您可能会遇到性能问题。

关于ios - 如何使用自动布局在自定义 UITableViewCell 中动态设置 UITextView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com