UITableViewAutomaticDimension无法正常工作。斯威夫特

时间:2018-08-29 08:40:38

标签: ios swift uitableview 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
}

}

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

enter image description here

一些更新!

我已经尝试将以下代码放入viewDidLoad()中:

tableView.rowHeight = 44
tableView.estimatedRowHeight = UITableViewAutomaticDimension

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

5 个答案:

答案 0 :(得分:6)

我建议您不要使用委托方法。

只需尝试在您的viewDidLoad中进行设置:

self.tableView.rowHeight = UITableViewAutomaticDimension;
// set estimatedRowHeight to whatever is the fallBack rowHeight
self.tableView.estimatedRowHeight = 44.0;

这总是对我有用。让我知道是否有帮助:)

答案 1 :(得分:1)

您做错了很多事情,但是 main 要点是您使用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
    }

}

结果:

enter image description here

答案 2 :(得分:0)

更新以下行:

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

收件人:

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

并在viewDidLoad中添加以下内容:

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;

答案 3 :(得分:0)

以下步骤可以解决您的问题:

1)在下面的单元格中为UILabel设置顶部,底部,前导和尾随约束: enter image description here

2)配置表格视图:

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;

答案 4 :(得分:0)

在Swift 5中:

func configureTableView() {
        myTableView.rowHeight =  UITableView.automaticDimension
        myTableView.estimatedRowHeight = 44
    }

请记住,如果.estimatedRowHeight不正确,Swift将为您进行数学计算。最后,在viewDidLoad()

中调用此方法