gpt4 book ai didi

ios - 如何创建具有动态数量标签的自定义 UITableViewCell

转载 作者:行者123 更新时间:2023-11-29 05:43:15 26 4
gpt4 key购买 nike

表格 View 中的每个自定义单元格可以有一个或两个或三个或更多标签,具体取决于从 api 返回的数据数量。

我尝试在 cellForRowAtIndexPath 方法中在单元格中创建动态数量的标签。但每次我滚动时,所有这些标签都会相互重叠。是否有其他方法可以在单元格中创建动态数量的标签?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PracLabelCell", for: indexPath) as! PracLabelCell

let w : CGFloat = CGFloat(self.tableview.frame.size.width/3)
var x : CGFloat = 0

for index in 0...2
{
let label = UILabel(frame: CGRect(x: x, y: cell.frame.origin.y, width: w , height: cell.frame.size.height))
let str = String(index)
print(str)
label.text = str
cell.contentView.addSubview(label)
print("x is" , x)
x += w

}

return cell
}

最佳答案

使用约束来增加单元格的高度。在单元格中添加垂直堆栈 View 。并在单元类中创建一个函数以根据参数值附加标签。

class PracLabelCell: UITableViewCell {
let titleLabel = UILabel()
let stackView = UIStackView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
titleLabel.translatesAutoresizingMaskIntoConstraints = false
addSubview(titleLabel)

stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 2
stackView.translatesAutoresizingMaskIntoConstraints = false
addSubview(stackView)

titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true

stackView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
stackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
func setup(names: [String]) {
stackView.arrangedSubviews.forEach { stackView.removeArrangedSubview($0);$0.removeFromSuperview() }
names.forEach {
let label = UILabel()
label.text = $0
stackView.addArrangedSubview(label)
}
}
}

并在tableView cellForRowAt方法中传递值。

class TableViewController: UITableViewController {
var namesArray = [["a","b","c"],["d","e"],["f"],["g","h"],[],["j"]]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
tableView.register(PracLabelCell.self, forCellReuseIdentifier: "PracLabelCell")
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return namesArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PracLabelCell", for: indexPath) as! PracLabelCell
cell.titleLabel.text = "This cell has \(namesArray[indexPath.row].count) labels"
cell.setup(names: namesArray[indexPath.row])
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}

enter image description here

关于ios - 如何创建具有动态数量标签的自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com