【发布时间】:2019-09-25 20:10:35
【问题描述】:
我想在集合视图中显示我的数据。
我有一个UICollectionView 并设置了cellForItemAt 委托方法。这是我在cellForItemAt委托方法中尝试过的代码。
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productCell", for: indexPath) as! ProductCollectionViewCell
编辑代码(所有cellForItemAt委托方法):
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productCell", for: indexPath) as! ProductCollectionViewCell
let productSnapshot = products[indexPath.item]
if let productsDictionary = productSnapshot.value as? NSDictionary{
guard let productName = productsDictionary["name"] as? String else { return UICollectionViewCell() }
guard let productPrice = productsDictionary["price"] as? Int else { return UICollectionViewCell() }
guard let productCategory = productsDictionary["category"] as? String else { return UICollectionViewCell() }
let product = Product(uid: productSnapshot.key, name: productName, price: productPrice, categoryUid: productCategory)
cell.setProduct(product: product)
}
return cell
}
这是错误信息:
“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'从-collectionView:cellForItemAtIndexPath:返回的单元格没有reuseIdentifier - 必须通过调用-dequeueReusableCellWithReuseIdentifier:forIndexPath:'来检索单元格”
【问题讨论】:
-
你能包括你的collectionView(cellForItemAtIndexPath:)函数的实现吗?还是那条线就是全部?
-
这是您在该函数中拥有的唯一代码吗?您可以编辑问题以完整显示该委托功能吗?您显示的代码与异常消息不一致
-
我编辑了代码。 @Paulw11
标签: ios swift uicollectionview