简体   繁体   中英

Label.text is nil

I am trying to show userNameLabel with content "aaaaaaaaaaaaaaaaA" but userNameLabel is 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)
    }
}

Additional code from comment:

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 
} 

Had same problem. In my case problem was in cell registration method. I was using register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String) but this was not working because my cell was created in xib. Using register(_ nib: UINib?, forCellReuseIdentifier identifier: String) helped me out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM