ios - Swift - UITableViewCell 中的自定义文本标签

标签 ios swift uitableview

目前我有一个 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/

相关文章:

ios - Codenameone ComponentGroup 未显示在真实的 ios 设备上

ios - 如何手动对选定的按键进行解码,以及如何利用快速解码功能自动解码呢?

iphone - 如何从 UITableView 获取行数

ios - 选项卡式应用程序和导航栏(带有标题和图标)

android - 使用配置文件发布移动(React Native) Artifact

ios - 解析 PFQuery IOS 返回用户名

ios - 在 Mac Catalyst 中访问 NSWindow

swift - didEnterRegion 和 didExitRegion 在 iOS8 中不起作用

ios - 自动滚动到具有特定值的单元格

ios - Swift 中 NSLayoutConstraints 的奇怪行为