自定义tableview控制器的单元格文本

时间:2020-12-13 10:43:55

I have a custom tableview controller which has 3 sections.

我有一个自定义的tableview控制器,有3个部分。

1 first section has a collection view. Now I want to add text (which length is less than 1000 characters) to a first cell of 2nd section.

1第一部分有一个集合视图。现在我想将文本(长度小于1000个字符)添加到第二部分的第一个单元格。

I plan to make that cell style basic, so I can add title.

我计划将该单元格样式设为基本,所以我可以添加标题。

  1. How can I call cell.title.text = myString?
  2. 我怎么称呼cell.title.text = myString?

I mean I want something like

我的意思是我想要类似的东西

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView.section == 2 and cell == 1 {
        cell.title.text = myString
        return cell
    }
}
  1. because myString length is between 0 ~ 1000 words. How can I change the height of cell base on the length of myString?
  2. 因为myString长度介于0~1000字之间。如何根据myString的长度更改单元格的高度?

Thanks

谢谢

1 个解决方案

#1


1  

First Question

First set an identifier for the cell. If you're using storyboards, put a value in the Identifier field in the Attributes Inspector when the cell is selected.

首先为单元格设置标识符。如果您正在使用故事板,请在选择单元格时在“属性”检查器的“标识符”字段中添加值。

Let's say you set that identifier to "basic". Then write something like the following code:

假设您将该标识符设置为“基本”。然后写下面的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 1 && indexPath.row == 0 {
        // Section and row numbers start from 0, like practically everything else
        // in Cocoa, so 1 is the second section, and 0 is the first row.
        let cell = tableView.dequeueReusableCell(withIdentifier: "basic")! // Get a cell from the table view
        cell.textLabel?.text = myString // Set the string
        return cell
    }
}

Second Question

To allow the text field to increase in height when the string is long, just set the numberOfLines property of the label in the cell to a large number (or a number that you calculate somehow based on the length of the string). For example, in the method above, before return cell, insert this line:

要在字符串很长时允许文本字段的高度增加,只需将单元格中标签的numberOfLines属性设置为一个大数字(或者根据字符串的长度以某种方式计算的数字)。例如,在上面的方法中,在返回单元格之前,插入以下行:

cell.textLabel?.numberOfLines = 1000

The numberOfLines property, according to its documentation, is the maximum number of lines to use for rendering text, so there shouldn't be anything wrong with setting it to a very large number. It will still resize based on the length of the string.

根据其文档,numberOfLines属性是用于呈现文本的最大行数,因此将其设置为非常大的数字应该没有任何问题。它仍将根据字符串的长度调整大小。

#1


1  

First Question

First set an identifier for the cell. If you're using storyboards, put a value in the Identifier field in the Attributes Inspector when the cell is selected.

首先为单元格设置标识符。如果您正在使用故事板,请在选择单元格时在“属性”检查器的“标识符”字段中添加值。

Let's say you set that identifier to "basic". Then write something like the following code:

假设您将该标识符设置为“基本”。然后写下面的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 1 && indexPath.row == 0 {
        // Section and row numbers start from 0, like practically everything else
        // in Cocoa, so 1 is the second section, and 0 is the first row.
        let cell = tableView.dequeueReusableCell(withIdentifier: "basic")! // Get a cell from the table view
        cell.textLabel?.text = myString // Set the string
        return cell
    }
}

Second Question

To allow the text field to increase in height when the string is long, just set the numberOfLines property of the label in the cell to a large number (or a number that you calculate somehow based on the length of the string). For example, in the method above, before return cell, insert this line:

要在字符串很长时允许文本字段的高度增加,只需将单元格中标签的numberOfLines属性设置为一个大数字(或者根据字符串的长度以某种方式计算的数字)。例如,在上面的方法中,在返回单元格之前,插入以下行:

cell.textLabel?.numberOfLines = 1000

The numberOfLines property, according to its documentation, is the maximum number of lines to use for rendering text, so there shouldn't be anything wrong with setting it to a very large number. It will still resize based on the length of the string.

根据其文档,numberOfLines属性是用于呈现文本的最大行数,因此将其设置为非常大的数字应该没有任何问题。它仍将根据字符串的长度调整大小。