Swift 3-自定义TableViewCell动态高度-以编程方式 [英] Swift 3 - Custom TableViewCell dynamic height - programatically
问题描述
我正尝试通过编程方式为自定义表格视图单元格设置动态高度。
I am trying to setup dynamic height for my custom table view cell...programatically.
在情节提要中有很多教程可以做到这一点,但这是关于纯编程解决方案:
There are many tutorials for doing it inside the storyboard, but this is about a pure programmatic solution:
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
}
}
我试图根据其高度限制简单地调整自定义表格视图单元格的大小:
I am trying to just simply resize my custom tableview cell bases on its height constraint:
self.container.heightAnchor.constraint(equalToConstant: 200)
但这不起作用。自定义表格视图单元格保持为 44 ..
But this not work.. the custom tableview cell stays at "44"..
有人能告诉我我纯粹的编程解决方案有什么问题吗?
Anybody could tell me what is wrong with my pure programmtic solution?
感谢和问候
推荐答案
问题似乎是高度限制未激活
The problem seems to be that the height constraint is not active
func addConstraints() {
...
self.container.heightAnchor.constraint(equalToConstant: 200)
}
这就是为什么 UITableViewAutomaticDimension
不起作用的原因。
Thats why the UITableViewAutomaticDimension
does not work.
将其更改为 self.container.heightAnchor.constraint(equalToConstant:200).isActive = true
并将其保存ld工作
Change it to self.container.heightAnchor.constraint(equalToConstant: 200).isActive = true
and it should work
这篇关于Swift 3-自定义TableViewCell动态高度-以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!