目前我有一个 UITableView
,用户可以在其中添加其中有 textLabel
的单元格
。
这就是我设置标签
的方式:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath)
let currentWish = self.wishList[indexPath.row]
cell.textLabel?.text = currentWish.wishName
cell.backgroundColor = .clear
return cell
}
我的问题是,如何自定义textLabel
(字体、约束...)。我尝试在 WishCell
类中创建自定义 UILabel
,但无法在 cellforRowAt
中使用 cell.theLabel
访问它。
我希望您理解我的问题,我非常感谢每一个帮助:)
已解决
我刚刚忘记了as!
。感谢您的帮助:)cellForRowAt
中的 WhishCell
最佳答案
您需要将其转换为单元格自定义类名
let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath) as! CellName
cell.lbl.text = ////
class CellName:UITableViewCell {
let lbl = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
lbl.backgroundColor = UIColor.red
self.contentView.addSubview(lbl)
// set constraints here
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
关于ios - Swift - UITableViewCell 中的自定义文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59057584/