【发布时间】:2015-09-14 05:54:12
【问题描述】:
我正在尝试在静态 UITableViewCell 多行中创建一个 UILabel,具体取决于它的内容,它会发生变化。
在viewDidLoad 方法中,我有初始化标签内文本的代码,紧随其后的是:
cellLabel.text = "Here is some text, but because of how long it is, it has to span a number of lines."
cellLabel.numberOfLines = 0
cellLabel.sizeToFit()
tableViewCell.textLabel?.sizeToFit()
tableViewCell.textLabel?.numberOfLines = 0
tableViewCell.textLabel?.lineBreakMode = .ByWordWrapping
然后我正在尝试计算该行必须达到的高度以容纳多行标签:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath.section == 2 && indexPath.row == 0) {
return cellLabel.frame.height
}
else {
return 44
}
}
然而,返回的是一个多行 UITableViewCell 但文本的结尾被截断并且不显示。我会在 return definitionLabel.frame.height 的末尾添加一个值,但是标签的内容会有所不同。
有人可以告诉我如何使用 Swift 2.0 和 iOS 9 在静态 UITableViewCell 中简单地拥有一个多行 UILabel。
注意:我已添加:
tableView.rowHeight = UITableViewAutomaticDimension
我正在使用一个基本的静态表格视图单元格,它默认应用了约束。
【问题讨论】:
-
与单元格的动态高度相同:tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 160.0
-
在不处理单元高度的情况下出售:raywenderlich.com/87975/…
-
我已经添加了 tableView.rowHeight = UITableVieeAutomaticDimension 并且没有运气。我有一个静态表格视图,所以只是使用一个具有自己约束的基本单元格。
-
你正在运行raywenderlich.com/87975/…的演示
-
你有解决办法吗?我也面临同样的挑战。我在静态表格单元格中有一个标签,我希望它是多行和自动高度。我已经尝试过
tableView.rowHeight = UITableViewAutomaticDimension和override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat。但就像你的一样,文本被截断了。
标签: ios swift uitableview swift2