【发布时间】:2024-01-20 13:53:01
【问题描述】:
我需要在我的UICollectionViewCell 中显示一个字符串。我创建了一个自定义视图单元格。但是每当我运行应用程序时,它都会显示“致命错误:在展开可选值时意外发现 nil”
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return places.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell
cell.layer.borderWidth = 0.5
cell.layer.borderColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor
//cell.placeLabel.tintColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor
cell.layer.cornerRadius = 35
cell.layer.masksToBounds = true
//cell.placeLabel.text = places[indexPath.row] as! String
cell.placeLabel.text = places[indexPath.row]
return cell
}
【问题讨论】:
-
您在哪一行得到错误?您还可以显示 PlaceCollectionViewCell 的代码吗?确保在情节提要中将单元格的标识符设置为“单元格”。
-
在“cell.placeLabel.text = places[indexPath.row]”这一行出现错误
-
您能否也显示您的 PlaceCollectionViewCell 代码?
-
@RakeshMohan 显示
places数组的初始化。 -
不,你的自定义collectionview类的代码,其中placeLabel被定义
标签: ios uicollectionview swift3 uicollectionviewcell