gpt4 book ai didi

swift - 如何仅以编程方式将控件添加到 tableview 单元格一次

转载 作者:行者123 更新时间:2023-11-28 10:48:54 24 4
gpt4 key购买 nike

override func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell    {let cell = tableView.dequeueReusableCell (withIdentifier: "cell", for: indexPath)
let customerNameLabel = UILabel()
customerNameLabel.frame = CGRect (x: 10, y: 5, width: 150, height: 40)
customerNameLabel.textColor = .black
customerNameLabel.text = "Customer Name\ (indexPath.row)"
cell.contentView.addSubview(customerNameLabel)
return cell
}

在尝试向单元格添加标签时,即使在滚动 tableView 时它也会添加次数,但我只需要添加标签一次并重新使用它

最佳答案

您需要创建一个自定义 UITableViewCell 并将您的标签添加到它。

因为在您提供的代码中,每次重复使用单元格时,您都会向单元格添加 UILabel()。

关于swift - 如何仅以编程方式将控件添加到 tableview 单元格一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47072715/

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