【发布时间】:2015-10-20 12:07:13
【问题描述】:
我正在创建 2 个部分 UITableView。第一部分使用UITableViewCells 和Default 样式,第二部分使用Subtitle 样式。两个单元格都可能有一个多行文本标签。
我已将表格的 rowHeight 设置为 UITableViewAutomaticDimension。
从下面的屏幕截图中可以看出,UITableViewAutomaticDimension 在使用Subtitle 样式时仅受到部分尊重:高度似乎适合textLabel 的高度,但不适合detailLabel 的高度。
如何使Subtitle 单元格获得正确的高度?
一些代码:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "defaultCell")
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section === 1 {
var cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "subtitleCell")
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = "A Long text..."
cell.detailTextLabel?.numberOfLines = 0
cell.detailTextLabel?.text = "A Long text..."
return cell
}
else {
var cell = tableView.dequeueReusableCellWithIdentifier("defaultCell") as! UITableViewCell
cell.textLabel!.text = "A long text..."
cell.textLabel?.numberOfLines = 0
return cell
}
}
我尝试实现UITableViewCell 的子类,但我在自动布局和章节标题方面存在很多问题(说来话长),所以我想知道是否可以用更简单的方式解决这个问题。
【问题讨论】:
-
你能解决这个问题吗?
标签: ios uitableview ios8 uikit