【发布时间】:2017-09-12 08:41:25
【问题描述】:
我正在尝试为我的自定义表格视图单元格设置动态高度...以编程方式。
故事板中有很多教程,但这是关于纯程序化解决方案的:
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
}
}
我正在尝试根据其高度约束简单地调整我的自定义表格视图单元格的大小:
self.container.heightAnchor.constraint(equalToConstant: 200)
但这不起作用..自定义表格视图单元格保持在“44”..
谁能告诉我我的纯程序解决方案有什么问题?
感谢和问候
【问题讨论】:
-
awakeFromNib不会被调用,除非您从笔尖/故事板创建单元格。覆盖init(style:reuseIdentifier:)并在那里设置您的约束。在表格中注册时还需要传递单元类而不是笔尖。 -
@dan 这应该是一个答案,而不是评论。
-
@dan awakeFromNib 被调用,因为当我打印某些内容时,它会显示在控制台中。您能否提供更多详细信息? ://
-
@dan 您能否根据我的代码更详细地参考您的解决方案?
-
awakeFromNib正在为此代码工作,因为 tableview 注册其单元格的方式。我猜@Creativecrypter 的代码不是很编程。可以改用self.tableview.register(CustomTableViewCell.self, forCellReuseIdentifier: "CellCustom")。
标签: swift uitableview dynamic nslayoutconstraint