I want to show my data in collections view.
I have a UICollectionView
and I set the cellForItemAt
delegate method. Here is the code I've tried in cellForItemAt
delegate method.
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productCell", for: indexPath) as! ProductCollectionViewCell
Edit Code (All of cellForItemAt Delegate Method):
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
}
This is the error message:
"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:'"