【发布时间】:2017-09-21 17:14:02
【问题描述】:
我用UICollectionView来显示我的数据,我用了custom cell,在cellForItemAt有趣的应用程序在我想给标签赋值时崩溃了。
let reuseIdentifier = "brandCell"
ViewDidLoad------>
self.brandCollectionView!.register(BrandCercaCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
brandCollectionView.dataSource = self
brandCollectionView.delegate = self
// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! BrandCercaCollectionViewCell
//App crashed here --->
cell.brandNameLabel.backgroundColor = UIColor.blue
return cell
}
我附上了故事板的照片。
【问题讨论】:
-
检查所有 IBOutlets 是否已连接...
-
你不是要让一个还不存在的单元格出队吗?
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! BrandCercaCollectionViewCell; if cell == nil { cell = BrandCercaCollectionViewCell(reuseIdentifier: reuseIdentifier)}或类似的东西 -
你在interface builder的字段中设置了可复用的标识符吗?
-
@GIJOW
dequeueReusableCell(withReuseIdentifier:for:)方法不返回可选项。 -
@AhmadF 它只是一个,你可以检查它的第一张图片
标签: ios swift uicollectionview custom-cell