iOS:具有多行的 UITableView 单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5964448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: UITableView cells with multiple lines?
提问by aneuryzm
What's the best way to have UITableView cells with multiple lines ? Let's say 5.. or 6 ?
拥有多行 UITableView 单元格的最佳方法是什么?让我们说 5.. 或 6 ?
Instead of textLabel and la detailTextLabel ? Should I create a custom style ? or a custom view ?
而不是 textLabel 和 la detailTextLabel ?我应该创建自定义样式吗?或自定义视图?
Any tutorial / example is well accepted.
任何教程/示例都被广泛接受。
thanks
谢谢
回答by rekle
You can use the existing UILabel views in the UITableViewCell for this. The secret is to do the following:
为此,您可以使用 UITableViewCell 中现有的 UILabel 视图。秘诀是做到以下几点:
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
By default, the UILabel only allows 1 line of text. Setting numberOfLines to 0 basically removes any limitations on the number of lines displayed. This allows you to have multiple lines of text.
默认情况下,UILabel 只允许 1 行文本。将 numberOfLines 设置为 0 基本上消除了对显示行数的任何限制。这允许您拥有多行文本。
The setting of the lineBreakMode to Word Wrap tells it to word wrap long lines of text onto the next line in the label. If you don't want this, you can skip that line.
将 lineBreakMode 设置为 Word Wrap 告诉它将长文本行自动换行到标签的下一行。如果你不想要这个,你可以跳过那一行。
You may also have to adjust the height of the table view cell as needed to make more room for the multiple lines of text you add.
您可能还需要根据需要调整表格视图单元格的高度,以便为您添加的多行文本腾出更多空间。
For iOS 6.0 and later, use NSLineBreakByWordWrapping
instead of UILineBreakModeWordWrap
, which has been deprecated.
对于 iOS 6.0 及更高版本,使用已弃用的NSLineBreakByWordWrapping
代替UILineBreakModeWordWrap
。
回答by Gobe
Since Swift 3:
从 Swift 3 开始:
func allowMultipleLines(tableViewCell: UITableViewCell) {
tableViewCell.textLabel?.numberOfLines = 0
tableViewCell.textLabel?.lineBreakMode = .byWordWrapping
}
回答by MikeWazowski
There is a method to accomplish this just using storyboard. First, select the cell and go to the attributes section on the right panel. The first option should be 'Style'. Change this from custom to basic. Now, in your cell you should see text that says 'Title'. Double click it and in the right panel you should be able to set the number of lines.
有一种方法可以仅使用情节提要来完成此操作。首先,选择单元格并转到右侧面板上的属性部分。第一个选项应该是“风格”。将此从自定义更改为基本。现在,在您的单元格中,您应该会看到显示“标题”的文本。双击它,在右侧面板中,您应该能够设置行数。
回答by Phil Cumpston
I found this worked for me on Xcode Version 8.0 (8A218a)
我发现这在 Xcode 版本 8.0 (8A218a) 上对我有用
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) UITableViewCell {
let cell = UITableViewCell()
//MARK: word wrapping in cell
cell.textLabel?.text = self.choices[(indexPath as NSIndexPath).row]
cell.textLabel?.numberOfLines=0 // line wrap
cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
return cell
}
回答by Allister Smith
cell.textLabel.numberOfLines = 0;
together with
和...一起
tableView.rowHeight = UITableViewAutomaticDimension
Does work but onlyif the number of lines is limited (2-3 lines).
有效,但前提是行数有限(2-3 行)。
What I had to do as well was embed the cell fields in a StackView
. That made all the difference. Now I can display as many lines as i want.
我还必须做的是将单元格字段嵌入到StackView
. 这让一切变得不同。现在,我想我可以显示尽可能多的行。