【问题标题】:Dynamice tableview Cell with Addsubview带有 Addsubview 的动态表格视图单元格
【发布时间】:2020-12-12 08:50:10
【问题描述】:

我需要在我的 tableview 单元格中添加多个复选框,以便单元格大小在运行时根据复选框按钮的数量自动增加。我该怎么做?

我的自定义单元格

import UIKit

class checkboxCell: UITableViewCell {

    @IBOutlet weak var txtLabel: UILabel!
    @IBOutlet weak var backView: UIView!
    @IBOutlet weak var checkBoxView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

我的显示单元格代码,运行时添加带有标签的复选框

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

............
.................
if indbexpath.row == 4
        {
            var checkBoxcell = tableView.dequeueReusableCell(withIdentifier:"checkboxCell") as? checkboxCell
            if checkBoxcell == nil{
                let arrNib:Array = Bundle.main.loadNibNamed("checkboxCell",owner: self, options: nil)!
                checkBoxcell = arrNib.first as? checkboxCell
            }
            checkBoxcell?.txtLabel.text = "Sample Checkbox"
            
            let checkBoxArray =  NSMutableArray(array:model.idnamemodelUI! as NSArray)
            
            for i in 0..<checkBoxArray.count {
                let checkbox = CheckBox(frame: CGRect(x: 10, y: i * 45, width: 30, height: 30))
                checkbox.checked = false
                checkbox.tag = "This is a label"
                 checkBoxcell?.contentView.addSubview(checkbox)
                let label = UILabel(frame: CGRect(x: 60, y: i * 45, width: 200, height: 30))
                label.textAlignment = .left
                label.text = labelName
                checkBoxcell?.contentView.addSubview(label)                
                
            }
            
            return checkBoxcell!
            
        }

............
.......
}


 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100 //66
    }

想要的输出寻找

【问题讨论】:

    标签: swift xcode uitableview uitableviewautomaticdimension


    【解决方案1】:

    方法 1:使用嵌套表格,在主表格视图单元格内添加另一个复选框表格,并使内部表格动态化 (https://medium.com/@dushyant_db/swift-4-recipe-self-sizing-table-view-2635ac3df8ab)。

    方法 2:使用堆栈视图。在内容视图中添加 UIStackView 并给出顶部、底部、左侧、右侧约束,然后将插座与单元格绑定,使用堆栈视图属性 addArrangedSubview 将您的 chack 框视图添加到堆栈视图中。

    方法 3(不推荐):在您的代码中,您使用了固定大小的复选框视图,因此在 heightForRowAt 方法中计算单元格的高度并返回计算的高度。例如假设复选框计数为 5,则最终高度为 (checkBoxCount * (fixedCheckBoxViewHeight + spaceBetweenTwoCheckBox))

    【讨论】:

    • 你能解释一下方法2:使用堆栈视图。在内容视图中添加 UIStackView 并给出顶部、底部、左侧、右侧约束,然后将插座与单元格绑定,使用堆栈视图属性 addArrangedSubview 将您的 chack 框视图添加到堆栈视图中。
    • 第二种方法,首先在cell XIB中添加一个verticle UIStackView,并绑定UIStackView的outlet。在单元格中为您创建复选框视图后,只需将该复选框视图添加到堆栈视图中即可。
    • 它会在同一个单元格上显示多个复选框并增加单元格大小吗?
    • 是的,它会根据您添加行条件的代码在同一个单元格上显示多个复选框。它会根据单元格的内容扩大单元格大小。
    • 我试过同样的方法它不起作用,任何样本都会有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2014-09-02
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多