我有一台tableViewCell
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")它具有以下设置:
cell.textLabel?.font = UIFont.boldSystemFontOfSize(15.0)
cell.textLabel?.text = titleString
cell.detailTextLabel?.text = descriptionString
cell.detailTextLabel?.numberOfLines = 0descriptionString由一个超文本标记语言字符串组成。
我设置好了
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}但是,当detailTextLabel大于1行时,这会导致混乱的表视图显示。detailTextLabel将包装到下一个表视图单元格上。
这个问题的解决方案是什么?UITableViewAutomaticDimension似乎没有按预期工作。
发布于 2016-06-25 22:32:48
表格视图单元格中的多行标签
首先定义
private let useAutosizingCells = true
override func viewDidLoad() {
super.viewDidLoad()
if useAutosizingCells && tableView.respondsToSelector(Selector("layoutMargins")) {
tableView.estimatedRowHeight = 88
tableView.rowHeight = UITableViewAutomaticDimension
}
}//标记:- UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
if useAutosizingCells && tableView.respondsToSelector(Selector("layoutMargins")) {
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0
}
return cell
}https://stackoverflow.com/questions/38029382
复制相似问题