UITableViewAutomaticDimension

UITableViewAutomaticDimension

阅读了Ray Wenderlich的“自定义表格视图单元格”的guide以及this question及其答案之后,我决定向大家寻求帮助。

有一个以编程方式创建的单元格:

import UIKit

class NotesCell: UITableViewCell {
lazy private var cellCaption: UILabel = {
    let label = UILabel()

    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping

    return label
}()

func configure(with note: NotesModel) {
    cellCaption.text = note.name

    contentView.addSubview(cellCaption)
}

override func layoutSubviews() {
    super.layoutSubviews()

    NSLayoutConstraint.activate([
        cellCaption.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
        cellCaption.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
        cellCaption.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
//            cellCaption.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
cellCaption.bottomAnchor.constraint(greaterThanOrEqualTo: contentView.bottomAnchor, constant: -8)
            ])

//        cellCaption.sizeToFit()
//        cellCaption.layoutIfNeeded()
}
}

表格视图控制器在委托方法中使用UITableViewAutomaticDimension:
extension NotesTableViewController {
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

}

结果,最长的字幕已完全显示,但该单元格的高度与其他所有单元格相同。

ios - UITableViewAutomaticDimension无法正常工作。 swift-LMLPHP

一些更新!

我已经尝试将以下代码放入viewDidLoad()中:
tableView.rowHeight = 44
tableView.estimatedRowHeight = UITableViewAutomaticDimension

同时启用和禁用委托方法。结果是一样的 :(

最佳答案

您做错了很多事情,但是要点是您对greaterThanOrEqualTo:的使用。

相反,它应该是:

cellCaption.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),

另外,您每次设置文本时,当前代码都会在子视图中添加新标签。单元被重用,因此您只想在创建单元时添加标签。

接下来,该表的正确属性是:
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44

将这两行放在表视图控制器的viewDidLoad()中,并且不要实现heightForRowAtestimatedHeightForRowAt函数。您可以完全删除extension

最后,您只需要设置一次约束。绝对是layoutSubviews()中的不是

这是一个完整的示例:
//
//  NotesTableViewController.swift
//
//  Created by Don Mag on 8/29/18.
//

import UIKit

class NotesModel: NSObject {
    var name: String = ""
}

class NotesCell: UITableViewCell {
    lazy private var cellCaption: UILabel = {
        let label = UILabel()

        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping

        return label
    }()

    func configure(with note: NotesModel) {
        cellCaption.text = note.name
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    func commonInit() -> Void {

        contentView.addSubview(cellCaption)

        NSLayoutConstraint.activate([
            cellCaption.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
            cellCaption.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
            cellCaption.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
            cellCaption.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
            ])

    }

}

class NotesTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 8
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NotesCell", for: indexPath) as! NotesCell

        let m = NotesModel()

        if indexPath.row == 3 {
            m.name = "This is a very long caption. It will demonstrate how the cell height is auto-sized when the text is long enough to wrap to multiple lines."
        } else {
            m.name = "Caption \(indexPath.row)"
        }

        cell.configure(with: m)

        return cell
    }

}

结果:

ios - UITableViewAutomaticDimension无法正常工作。 swift-LMLPHP

关于ios - UITableViewAutomaticDimension无法正常工作。 swift ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52073107/

10-08 20:52