我正在尝试使静态单元格中的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,但是文本的结尾被截断了,没有显示出来。我想在返回定义的末尾添加一个值definitionLabel.frame.height,但是标签的内容是不同的。
请问有谁能告诉我如何在Swift 2.0和iOS 9中简单地使用静态UITableViewCell创建多行UILabel?
注:我已经添加了:
tableView.rowHeight = UITableViewAutomaticDimension
我正在使用一个基本的静态表格视图单元格,其默认应用了约束。
tableView.rowHeight = UITableViewAutomaticDimension
和override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
。但是和你的一样,文本被截断了。 - Sayaneeself.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44.0;
- Nilesh