【发布时间】: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