繁体   English   中英

UICollectionViewCell 中的 UILabel 为 nil

[英]UILabel inside UICollectionViewCell is nil

设置值时,UICollectionViewCell 内的 Label 为 nil。 因此,使用 storyboard 中的自定义单元格,不在 viewDidLoad 中注册。 下面是我的代码:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalenderCell", for: indexPath) as! CalenderCollectionViewCell
    
   
    if let label = cell.lblDayName{
        label.text = arrCalendarDates[indexPath.item] // crashing here thus used if let to avoid crash but it is nil, Why so?
    }
   
    return cell
}

更新:

class CalenderCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var lblDayName: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

在情节提要中,UICollectionView

在自定义 cell.xib

您应该检查@IBOutlet是否连接到 storyboard 中的预期UILabel

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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