代码之家  ›  专栏  ›  技术社区  ›  Pascal

更改UITableViewCell中包装的视图的高度约束

  •  1
  • Pascal  · 技术社区  · 6 年前

    显示视图后,我想更改嵌套视图的高度。

    我正在使用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")
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Glenn Posadas    6 年前

    你不会想那样做的。据我所知,那是行不通的。您需要在单元格的 init . 或者在你的 cellForRow 功能。

    所以在下面的代码中,我修改了你的代码,并添加了你的属性(高度约束) NestedView TestCell 牢房。我更新了里面的高度 牢房

    编辑:如果你想切换一个单元格的高度,当然你要用同样的方法,你只需要重新加载你的tableView的行/节或整个数据。

    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
        }
    }
    

    测试单元.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")
        }
    }
    

    嵌套视图.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")
        }
    }
    
    推荐文章