首页 > 解决方案 > Label.text 为零

问题描述

我正在尝试显示userNameLabel内容“aaaaaaaaaaaaaaaaA”,userNameLabel但是nil.

class ChooseMemberTableViewCell: UITableViewCell, Reusable {

    @IBOutlet weak var userImageView: UIImageView!

    @IBOutlet weak var userNameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

    func setupCell(data: User) {
        userNameLabel?.text = "aaaaaaaaaaaaaaaaA"
//        userNameLabel?.text = "\(data.userName)"
//        let url = URL(string: data.image)
//        userImageView.sd_setImage(with: url ?? "", completed: nil)
    }
}

评论中的附加代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = listContacts.dequeueReusableCell(for: indexPath, cellType: ChooseMemberTableViewCell.self).then { 
        let user = searchUser[indexPath.row] 
        $0.setupCell(data: user) 
    } 
    return cell 
} 

标签: iosswiftiphone

解决方案


有同样的问题。就我而言,问题在于单元注册方法。我正在使用register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String),但这不起作用,因为我的单元格是在 xib 中创建的。使用register(_ nib: UINib?, forCellReuseIdentifier identifier: String)帮助了我。


推荐阅读