Ios UICollectionViewCell内的UILabel为零
设置值时,UICollectionViewCell内的标签为零。使用故事板中的自定义单元格,因此不在viewDidLoad中注册。下面是我的代码:Ios UICollectionViewCell内的UILabel为零,ios,swift,uicollectionviewcell,iboutlet,Ios,Swift,Uicollectionviewcell,Iboutlet,设置值时,UICollectionViewCell内的标签为零。使用故事板中的自定义单元格,因此不在viewDidLoad中注册。下面是我的代码: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell
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
}
您应该检查
@IBOutlet
是否连接到故事板中预期的UILabel
。检查dequeueReusableCellWithReuseIdentifier中的reuseIdentifier与故事板中的reuseIdentifier是否相同。是否在单元中连接了插座lblDayName
?是,都已检查,正确的标识符和连接的插座,否则如果让它进入,它会崩溃吗?如果是,那么不是标签,而是零。尝试arrCalendarDates[indexPath.row]是否将单元格注册到集合视图?它已连接,先生,也已重新检查。这可能是愚蠢的。在这种情况下,您确定@IBOutlet
连接到正确的目标UICollectionViewCell
子类,而不是UIViewController
子类吗?是的,共享,自定义类,更新了问题