【问题标题】:Wrong height with automatic sizing tableView自动调整大小 tableView 的高度错误
【发布时间】:2018-11-06 11:57:25
【问题描述】:

场景

我在 UITableView 上使用了 automaticDimension 选项。我想在我的单元格中有一个 UILabel,它可以自动调整大小以适应文本。

设置

在这里你可以看到我是如何设置标签的。边缘等于 contentView 的边距。

问题

当文字适合一行时,手机上的高度设置为 37.0 磅。 44.0应该是最低的。

问题

如何设置布局以保持最小单元格高度为 44.0(默认高度,适合其他单元格)?

编辑:

使用 numberOfLines = 0 的内置“基本”TableViewCell 似乎是最简单和最好的解决方案!由 eddwinpaz 建议。谢谢大家。

【问题讨论】:

  • 尝试设置标签的numberOfLine = 0
  • 设置高度约束 GreaterThanOrEqualToConstant = 44.0.
  • 如果是带有标签的单个单元格,则不需要使用 UILabel。只需使用 UITableviewCell 默认文本标签。并为该文本标签使用添​​加无限行。 cell.textlabel?.numberOfLine = 0
  • 这是一个很好的解决方案。有时最好的解决方案是最简单的。

标签: ios swift uitableview uitableviewautomaticdimension


【解决方案1】:

将顶部锚点从constraint(equalTo:) 更改为greaterThanOrEqualTo:,将底部锚点更改为lessThanOrEqualTo:。 然后使标签以Y-Axis 为中心。

那你应该设置cell. heightAnchor.constraint(greaterThanOrEqualToConstant: 44)

【讨论】:

  • 当需要多行时,这不会使文本也以 Y 轴为中心吗?可以这么说,我希望文本从第一行开始
  • 你是什么意思。在第一行?文本附在顶部?如果是:将底部锚点从约束(equalTo:) 更改为 lessThanOrEqualTo。然后你应该设置单元格。 heightAnchor.constraint(greaterThanOrEqualToConstant: 44)
【解决方案2】:

如果您只使用单个 UILabel,则需要使用 numberOfLine = 0。别的。您需要使用约束。

import UIKit

class TableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 5
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    cell.textLabel?.numberOfLines = 0
    cell.textLabel?.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

    return cell
}

}

如果您需要像您的案例一样使用自定义 UILabel。

class TableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(CustomCell.self, forCellReuseIdentifier: "reuseIdentifier")
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 5
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! CustomCell
    cell.myLabel.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

    return cell
}

 }

class CustomCel: UITableViewCell {

let myLabel: UILabel = {
   let label = UILabel()
    label.numberOfLines = 0
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

func setupViews(){
    addSubview(myLabel)

    myLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    myLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    myLabel.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    myLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

 }
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

【讨论】:

  • 我想保持单元格边缘的边距,这在只有一行文本时会起作用(并且文本垂直居中)。
  • @Josh 添加一个常量 myLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多