高度不正确,带有自动尺寸表

时间:2018-11-06 11:57:25

标签: ios swift uitableview uitableviewautomaticdimension

场景

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

设置

Autolayout config 在这里您可以看到如何设置标签。边缘等于contentView的边距。

问题

当文本适合一行时,手机上的高度设置为37.0点。最低应为44.0。

问题

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

编辑:

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

2 个答案:

答案 0 :(得分:0)

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

然后您应该设置cell. heightAnchor.constraint(greaterThanOrEqualToConstant: 44)

答案 1 :(得分:0)

如果您仅使用Single 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")
}
}
相关问题