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

问题描述:

我想为我的自定义表格视图单元格...编程设置动态高度。Swift 3 - 自定义TableViewCell动态高度 - 以编程方式

有做它的故事板里面很多教程,但是这是关于一个纯粹的编程解决方案:

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”..

任何人都可以告诉我什么是我的纯programm解决方案错?

感谢和问候

+1

'awakeFromNib'不会被调用,除非你是从笔尖/故事板创建细胞。重写'init(style:reuseIdentifier:)'并在那里设置你的约束。在表中注册时,您还需要传递单元类而不是笔尖。 – dan

+0

@dan这应该是一个答案,而不是评论。 – rmaddy

+0

@dan awakeFromNib被调用,因为当我打印某些内容时,它会显示在控制台中..您能否给出一些更详细的信息? :/ –

这个问题似乎是高度约束是不活跃

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

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

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

+0

你是男人 –

+0

在那里哈哈哈。 – hfehrmann

就动态地计算每个单元的高度。

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

PS:最好保存这个高度值,所以你不需要重复计算它一遍又一遍。