【问题标题】:UILabel inside UICollectionViewCell is nilUICollectionViewCell 中的 UILabel 为 nil
【发布时间】:2021-06-04 14:53:28
【问题描述】:

UICollectionViewCell 中的标签在设置值时为零。因此,使用故事板中的自定义单元格,而不是在 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
}

【问题讨论】:

  • 检查 dequeueReusableCellWithReuseIdentifier 和情节提要中的重用标识符是否相同。
  • 插座lblDayName是否连接在单元格中?
  • 是的,都检查了,正确的标识符和插座也连接了,否则它会崩溃
  • 如果让它进去吗?如果是,那么它不是 nil 的标签。试试 arrCalendarDates[indexPath.row]
  • 您是否将单元格注册到集合视图?

标签: ios swift uicollectionviewcell iboutlet


【解决方案1】:

您应该检查@IBOutlet 是否连接到情节提要中预期的UILabel

【讨论】:

  • 已连接,先生,也已重新检查
  • 这可能是愚蠢的。你确定@IBOutlet在这种情况下连接到正确的目标UICollectionViewCell子类而不是UIViewController子类?
  • 是共享的,自定义类也是,更新的问题
猜你喜欢
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多