【发布时间】:2021-10-12 17:02:54
【问题描述】:
我正在创建一个单元格具有自动高度的 UITableView。每个单元格都有 3 个使用自动布局的 UILabel:
我认为我已将所有约束设置得很好,但是我遇到了一些奇怪的事情,并且事件标签的某些单元格的高度超出了预期:
有些行的高度可以,但有些行不行,Event lavel 的高度可能会更低。
override func viewDidLoad() {
super.viewDidLoad()
// First load table nib
let bundle = Bundle(for: type(of: self))
// Register table cell class from nib
let cellNib = UINib(nibName: "OnlineEventsCell", bundle: bundle)
tableView.register(cellNib, forCellReuseIdentifier: "onlineEventsCell")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tableView.estimatedRowHeight = 59.0; // set to whatever "average" cell height is
self.tableView.rowHeight = UITableView.automaticDimension;
self.navigationItem.title = NSLocalizedString("ActiveEvents", comment: "")
searchBar.delegate=self
self.refreshControl?.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
loadViewData(reloading: false, eventName: nil)
self.tableView.reloadData()
}
【问题讨论】:
-
您是否尝试在
viewDidLoad()中设置estimatedRowHeight和rowHeight?
标签: ios swift xcode uitableview autolayout