【发布时间】: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