【发布时间】:2018-07-13 17:00:03
【问题描述】:
那里的解决方案似乎对我不起作用。
在上图中,我有一个简单的单元格。 在点击单元格时,我想将 redView 的约束更改为更大。这应该会自动改变单元格的高度。
我已经将单元格的高度限制设置为@IBOutlet,我认为我正确地更改了单元格的大小,但它不起作用。
这是我无法运行的示例应用程序。有什么帮助吗? SampleApp - for Xcode 9.3
【问题讨论】:
标签: ios swift uitableview
那里的解决方案似乎对我不起作用。
在上图中,我有一个简单的单元格。 在点击单元格时,我想将 redView 的约束更改为更大。这应该会自动改变单元格的高度。
我已经将单元格的高度限制设置为@IBOutlet,我认为我正确地更改了单元格的大小,但它不起作用。
这是我无法运行的示例应用程序。有什么帮助吗? SampleApp - for Xcode 9.3
【问题讨论】:
标签: ios swift uitableview
您需要为红色视图设置底部约束,以便自动布局可以在设置常量值后拉伸单元格
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath) as! customcell
configure(cell: cell, indexPath: indexPath)
cell.redview.backgroundColor = .red
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! customcell
cell.constraint.constant = data[indexPath.row] == "contracted" ? 30 : 200
data[indexPath.row] = data[indexPath.row] == "contracted" ? "expanded" : "contracted"
tableView.reloadData()
}
func configure(cell: customcell, indexPath: IndexPath) {
let data = self.data[indexPath.row]
if data == "expanded" {
cell.constraint.constant = 200
} else {
cell.constraint.constant = 30
}
cell.layoutIfNeeded()
}
}
【讨论】:
beginUpdates & endUpdates 组合优于您的解决方案。重新加载整个表通常会产生其他严重影响。 layoutIfNeeded 无论如何都不需要。
调用下面将重新计算高度。
[tableView beginUpdates];
[tableView endUpdates];
【讨论】: