ios - 更改包装在 UITableViewCell 中的视图的高度约束
问题描述
我有一个 UITableView 和一个包含视图的自定义单元格。
显示视图后,我想更改嵌套视图的高度。
我正在使用 SnapKit 框架的 updateConstraints 函数。
但是,我得到错误,更新的约束与UIView-Encapsulated-Layout-Height
显然来自初始设置高度的约束冲突。
"<SnapKit.LayoutConstraint:0x2807666a0@ViewController.swift#89 SnapKitTest.NestedView:0x102005ef0.height == 100.0>",
"<SnapKit.LayoutConstraint:0x280766880@ViewController.swift#70 SnapKitTest.NestedView:0x102005ef0.top == SnapKitTest.TestCell:0x10280e200.top>",
"<SnapKit.LayoutConstraint:0x280766940@ViewController.swift#70 SnapKitTest.NestedView:0x102005ef0.bottom == SnapKitTest.TestCell:0x10280e200.bottom>",
"<NSLayoutConstraint:0x280058a50 'UIView-Encapsulated-Layout-Height' SnapKitTest.TestCell:0x10280e200.height == 20 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:0x2807666a0@ViewController.swift#89 SnapKitTest.NestedView:0x102005ef0.height == 100.0>
下面是一些运行示例应用程序的源代码:
import UIKit
import SnapKit
class ViewController: UIViewController {
let tableView = UITableView.init(frame: .zero)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.edges.equalTo(self.view)
}
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "X")
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 100
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
if let c = tableView.cellForRow(at: IndexPath(row: 5, section: 0)) as? TestCell {
c.nestedView.snp.updateConstraints { make in
make.height.equalTo(100)
}
} else {
print("didnt geht cell")
}
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 5 {
let c = TestCell.init(style: .default, reuseIdentifier: nil)
return c
}
let c = tableView.dequeueReusableCell(withIdentifier: "X")
c?.backgroundColor = .orange
return c!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
}
class TestCell: UITableViewCell {
let nestedView = NestedView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(nestedView)
nestedView.snp.makeConstraints { make in
make.edges.equalTo(self)
}
backgroundColor = .blue
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class NestedView: UIView {
init() {
super.init(frame: .zero)
backgroundColor = .yellow
snp.makeConstraints { make in
make.height.equalTo(20)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
解决方案
你不想那样做。据我所知,这是行不通的。您需要在单元格的init
. 或在您的cellForRow
功能中。
因此,在下面的代码中,我修改了您的代码,并NestedView
在您的TestCell
单元格中添加了您的属性(高度约束)。我更新了cellForRow
.
编辑:如果你想切换一个单元格的高度,当然你也可以这样做,你只需要重新加载 tableView 的行/部分或整个数据。
ViewController.swift
import UIKit
import SnapKit
class ViewController: UIViewController {
let tableView = UITableView.init(frame: .zero)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.edges.equalTo(self.view)
}
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "X")
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 100
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 5 {
let c = TestCell.init(style: .default, reuseIdentifier: nil)
c.constraint_HeightOfNestedView?.update(offset: 100.0)
return c
}
let c = tableView.dequeueReusableCell(withIdentifier: "X")
c?.backgroundColor = .orange
return c!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
}
TestCell.swift
import SnapKit
class TestCell: UITableViewCell {
var constraint_HeightOfNestedView: Constraint?
let nestedView = NestedView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(nestedView)
nestedView.snp.makeConstraints { make in
make.edges.equalTo(self)
self.constraint_HeightOfNestedView = make.height.equalTo(20.0).priority(999).constraint
}
backgroundColor = .blue
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
NestedView.swift
import UIKit
class NestedView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .yellow
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
推荐阅读
- jenkins - 有没有办法可以将我的机器人测试结果从 Jenkins 报告给 slack?
- laravel - 如何获得 Laravel 数据库的总和值?
- ajax - 本地主机拒绝连接 - ERR_CONNECTION_REFUSED
- python - Plotly:如何使用 plotly.express 饼图向 hover_data 添加元素?
- smlnj - number_in_month 练习(尝试构建列表的 SML 函数中的 Tycon 不匹配错误)
- sharepoint - 错误:找不到“恢复”的重载和参数计数:“1”
- excel - 我们发现 (filename.xlsm) 中的某些内容存在问题
- python-3.x - 在神经网络中使用自定义激活函数时的精度差异
- javascript - Adobe Lifecycle Designer 中的下划线字符串
- mysql - 使用批处理文件执行mysql命令