Reputation: 1514
Actually I develop a Wishlist page for an e-comerce app. The Wishlist cell contains a Love(Wishlist) button.
class WishCell: UICollectionViewCell {
var wishButtonTapped : (() -> Void)? = nil
@IBAction func wishBtnTapped(_ sender: Any) {
if let wishBtnTap = wishButtonTapped {
wishBtnTap()
}
}
}
Button click is handled in CellForRowAt.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WishCell", for: indexPath) as? WishCell {
cell.configureCell(wishlist: wishlists[indexPath.row])
// Wish Button
cell.wishButtonTapped = {
print("indexPath", indexPath)
print("indexPathRow", indexPath.row, wishlistJSON.count)
// wishlistJSON.remove(at: indexPath.row)
// self.parseWishlistJSON()
self.wishlists.remove(at: indexPath.row)
collectionView.deleteItems(at: [indexPath])
// self.collectionView.reloadData()
self.updateDesign()
}
return cell
} else {
return UICollectionViewCell()
}
}
tap on third cell
indexPath [0, 2]
indexPathRow 2 5
tap again on third cell
indexPath [0, 3]
indexPathRow 3 5
wishlistJSON.remove(at: indexPath.row)
because the array indexOutOfBoundUpvotes: 3
Views: 1882
Reputation: 600
I had the same issue and for me, forcing a collectionView.reloadData() did the trick. Still unclear why the deletion of a row doesn't update the IndexPath for the cells...but at least this got it working
Upvotes: 1
Reputation: 768
Reloading the Collection-view must do the trick
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
}
Upvotes: 0
Reputation: 1514
When cells get reused, cell.wishButtonTapped actions will get added again! That's not what we want.
protocol WishlistDelegate: class {
func wishlistBtnTap(_ sender: WishCell)
}
class WishCell: UICollectionViewCell {
weak var delegate: WishlistDelegate?
@IBAction func wishBtnTapped(_ sender: Any) {
delegate?.wishlistBtnTap(self)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WishCell", for: indexPath) as? WishCell {
cell.configureCell(wishlist: wishlists[indexPath.row])
cell.delegate = self
return cell
} else {
return UICollectionViewCell()
}
}
extension WishlistVC: WishlistDelegate {
func wishlistBtnTap(_ sender: WishCell) {
guard let tappedIndexPath = collectionView.indexPath(for: sender) else { return }
// Delete the Item
wishlists.remove(at: tappedIndexPath.row)
collectionView.deleteItems(at: [tappedIndexPath])
}
}
Upvotes: 0