Swift 3 - 自定义TableViewCell动态高度 - 以编程方式

时间:2017-09-11 22:17:25

标签: swift uitableview dynamic nslayoutconstraint

我正在尝试为我的自定义表格视图单元格设置动态高度...以编程方式。

在故事板中有很多教程可以做,但这是一个纯粹的程序化解决方案:

class CustomTableViewCell: UITableViewCell {

    var container: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints =  false
        return view
    }()

    override func awakeFromNib() {
        super.awakeFromNib()

        self.setupView()
        self.addConstraints()
    }

    func setupView() {
        self.contentView.addSubview(self.container)
        self.container.backgroundColor = UIColor.cyan
    }

    func addConstraints() {
        self.container.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 0).isActive = true
        self.container.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 0).isActive = true
        self.container.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive = true
        self.container.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0).isActive = true
        self.container.heightAnchor.constraint(equalToConstant: 200)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

我的观点在于我注册的地方:

class CustomViewController: UIViewController {

    var data: [Post] = []

    let tableview: UITableView = {
        let tableview = UITableView()
        tableview.translatesAutoresizingMaskIntoConstraints =  false
        return tableview
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupView()
        self.addConstraints()
        self.getData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func setupView() {
        self.view.addSubview(self.tableview)
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CellCustom")
        self.tableview.estimatedRowHeight = 100
        self.tableview.rowHeight = UITableViewAutomaticDimension
    }

    func addConstraints() {
        self.tableview.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 40).isActive = true
        self.tableview.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -40).isActive = true
        self.tableview.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 40).isActive = true
        self.tableview.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -40).isActive = true
    }

    func getData() {
        // some networking stuff
    }

}

extension CustomViewController: UITableViewDelegate {

}

extension StreamViewController: UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

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

        return cell
    }

}

我只想简单地调整自定义tableview单元格的高度约束:

self.container.heightAnchor.constraint(equalToConstant: 200)

但这不起作用..自定义tableview单元格保持在“44”..

任何人都可以告诉我我的纯粹的程序化解决方案有什么问题?

谢谢和问候

2 个答案:

答案 0 :(得分:2)

问题似乎是高度限制不活跃

func addConstraints() {
    ...
    self.container.heightAnchor.constraint(equalToConstant: 200)
}

这就是为什么UITableViewAutomaticDimension不起作用。

将其更改为self.container.heightAnchor.constraint(equalToConstant: 200).isActive = true,它应该可以正常工作

答案 1 :(得分:0)

只需动态计算每个细胞的高度。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 200
}

PS:最好保存这个高度值,这样你就不需要反复重新计算它。

相关问题