[英]Swift - Custom textLabel in TableViewCell
at the moment I have a UITableView
where the user can add cells
that have a textLabel
inside of it.目前我有一个UITableView
,用户可以在其中添加其中包含textLabel
cells
。
That is how I set the label
:这就是我设置label
:
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
}
My question is, how I can custom textLabel
(font, constraints,...).我的问题是,如何自定义textLabel
(字体、约束等)。 I tried creating a custom UILabel
inside my WishCell
class but I can not access it in cellforRowAt
with cell.theLabel
.我尝试在我的WishCell
类中创建一个自定义UILabel
但我无法在cellforRowAt
使用cell.theLabel
访问它。
I hope you understand my problem, I am very grateful for every help :)我希望你理解我的问题,我非常感谢每一个帮助:)
SOLVED解决了
I just forgot the as! WhishCell
我只是忘记了as! WhishCell
as! WhishCell
in cellForRowAt
. cellForRowAt
as! WhishCell
。 Thanks for all your help :)感谢你的帮助 :)
You need to cast to you cell custom class name您需要将自定义类名称转换为单元格
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)
}
}
Dont force un-wrap it.. try using guard
or if let
不要强行解开它.. 尝试使用guard
或if let
guard let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath) as? WhishCell else{
return UITableViewCell()
}
cell.lbl.text = "my text"
let currentWish = self.wishList[indexPath.row]
cell.textLabel?.text = currentWish.wishName
cell.backgroundColor = .clear
return cell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.