作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
我是一名优秀的程序员,十分优秀!