Swift 根据文本长度动态增长单元格 [英] Swift Dynamically growing a cell depending on the text length

查看:31
本文介绍了Swift 根据文本长度动态增长单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 个部分的 TableViewController:第 1 部分 - 是从 xib 文件加载的单元格.该单元格仅包含一个 TextView.第 2 部分 - 包含从数组填充的多个单元格.

I have a TableViewController which contains 2 sections: Section 1 - is a cell which is loaded from a xib file. This cell just contains a TextView. Section 2 - Contains multiple cells populated from an Array.

仅当您选择的主(前一个)UITableView 单元格包含特定数据时,第 1 部分才存在.

The section 1 only exists if the master (previous) UITableView cell you select contains a certain piece of data.

以上所有都按预期工作,下面是父视图.项目列表来自数据库,有些项目有描述,有些没有.例如,在此图像下方,您将看到已选择经典初学者"的视图.然后在其下方,如果选择了炒菜",您将看到该视图.Stri Frys 包含说明:

All of the above works as expected, below is the parent view. The list of items come from a database, some items have a description, and some do not. For example below this image, you'll see the view is 'Classic Starters' is selected. Then below that, you'll see the view if 'Stir Frys' is selected. Stri Frys contains a description:

现在,我想要的是,显示在Stir Frys 页面上的描述单元格,根据其中文本的长度自动增长.所以如果一个描述有 10 行长,它会增长到显示所有 10 行.

Now, what I want is, the description cell which is shows on the Stir Frys page, to automatically grow depending on the length of the text inside it. So if a description is 10 lines long, it will grow to show all 10 lines.

这是否必须以编程方式完成,还是我缺少它们在 XCode 中的功能?

Does this have to be done programmatically, or is their a feature in XCode I'm missing ?

推荐答案

首先您需要做的是将 estimatedRowSize 设置为最能估计最常见大小的值,和 rowSizeUITableViewAutomaticDimension:

First you need to do is to set estimatedRowSize to a value that best estimates most common size, and rowSize to UITableViewAutomaticDimension:

self.tableView.estimatedRowHeight = 44
self.tableView.rowHeight = UITableViewAutomaticDimension

在您的情况下,由于除了第一个单元格之外,所有其他单元格都应该相同,您可以使用其余单元格的高度作为 estimatedRowHeight.

In your case, since except the first one all the other cells are supposed to be the same, you can use the height of the rest of the cells as the estimatedRowHeight.

您根本不需要实现 heightForRowAt.

您需要做的第二个步骤是对单元格设置适当的约束.这意味着您必须将单元格的内容限制在单元格的左侧、右侧、顶部和底部,这样当内容增长时,单元格也需要增长.常见的错误是忘记约束底部,这样单元格就不会生长,内容从单元格底部泄漏.

The second step you need to do is to setup proper constraints on the cells. That means you have to constrain the contents of the cell to the left, right, top and bottom of the cell, so that when contents grow, the cell will need to grow, too. Common mistake is to forget to constrain bottom, so then the cell does not grow and the contents leak through the bottom of the cell.

第三,由于您的动态单元格包含 UITextView,您需要确保它会随文本一起增长.那不是自动的.为了实现这一点,基于这个答案,这应该足够了(在单元格中):

Third, since your dynamic cell contains UITextView, you need to make sure that it will grow with its text. That is not automatic. To achieve that, based on this answer, this should suffice (in the cell):

textView.isScrollEnabled = false

如果您使用的是故事板,只需取消选中启用滚动.

If you are using storyboard, just uncheck scroll enabled.

这篇关于Swift 根据文本长度动态增长单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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