如何根据字符串大小在 UITableViewCell 中动态设置 UITextView 的高度? [英] How to set UITextView's height dynamically in UITableViewCell based on string size?

查看:28
本文介绍了如何根据字符串大小在 UITableViewCell 中动态设置 UITextView 的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有人问过这个问题

每篇文章都包含这些 UI 元素.唯一可以使每个单元格高度不同的元素是 messageView,因为它的高度取决于正在显示的字符串.问题是,如何动态设置每个单元格的高度?这是我现在所拥有的(不起作用,messageView 根本没有显示):

func cellForRowAt(indexPath: IndexPath) ->UITableViewCell {让 cell = tableView.dequeueReusableCell(...) 作为!后单元格让消息 = ...cell.messageView.text = 消息返回单元格}func heightForRowAt(indexPath: IndexPath) ->CGFloat {var cellMinimumHeight: CGFloat = 120if let cell = tableView.cellForRow(at: indexPath) as?后单元{让大小 = cell.messageView.sizeThatFits(cell.messageView.frame.size)cellMinimumHeight += size.height}返回单元格最小高度}

heightForRowAt函数中,if没有被执行,因此,所有单元格的高度都是cellMinimumHeight = 120.

如何让每个单元格的高度 = 120 + messageView 的高度?

---------------------------------编辑 1---------------------------------

图片弄错了,messageView的高度没有设置

解决方案

当使用自动布局动态调整单元格大小时,您实际上并不需要实现 sizeThatFits(...).如果约束设置正确,那么您只需要禁用 UITextView 的滚动.

来自代码:

yourTextView.scrollEnabled = false

来自 IB:

选择您的文本视图并打开属性检查器,然后

I understand that this question has been asked here, but it didn't solve my problem as the link in the accepted answer is down and the minimal example didn't help.

Here is a picture of my custom UITableViewCell & all its constraints:

Each post contains these UI elements. The only element that could make each cell's height different is messageView, because its height depends on the string being displayed. Question is, how do I dynamically set each cell's height? Here's what I have now (Does NOT work, messageView is not shown at all):

func cellForRowAt(indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(...) as! PostCell

    let message = ...
    cell.messageView.text = message

    return cell
}

func heightForRowAt(indexPath: IndexPath) -> CGFloat {
    var cellMinimumHeight: CGFloat = 120
    if let cell = tableView.cellForRow(at: indexPath) as? PostCell {
        let size = cell.messageView.sizeThatFits(cell.messageView.frame.size)
        cellMinimumHeight += size.height
    }

    return cellMinimumHeight
}

in heightForRowAt function, the if is not being executed, therefore, all cells' heights are cellMinimumHeight = 120.

How could I make each cell's height = 120 + messageView's height?

---------------------------------EDIT 1---------------------------------

Made a mistake in the picture, messageView's height is not set

解决方案

When using Auto-Layout for dynamically sizing cells, you don't really need to implement sizeThatFits(...). If the constraints are setup correctly, then you only need to disable the scrolling of the UITextView.

From code:

yourTextView.scrollEnabled = false

From IB:

Select your Text View and open Attributes inspector, then

这篇关于如何根据字符串大小在 UITableViewCell 中动态设置 UITextView 的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆