使用swift从中删除项目后,CollectionView indexPath不会更新

问题描述 投票:0回答:2

实际上,我为电子商务应用开发了愿望清单页面。愿望清单单元格包含一个爱(愿望清单)按钮。

Requirement

  1. 点击Love(Wishlist)按钮时删除单元格
  2. 很好的删除动画

enter image description here

WishCell - button click part

class WishCell: UICollectionViewCell {

    var wishButtonTapped : (() -> Void)? = nil

    @IBAction func wishBtnTapped(_ sender: Any) {
        if let wishBtnTap = wishButtonTapped {
            wishBtnTap()
        }
    }
}

CollectionView CellForRowAt

按钮单击在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()
        }
    }

Print Log

点击第三个细胞

indexPath [0, 2]
indexPathRow 2 5

再次点击第三个细胞

indexPath [0, 3]
indexPathRow 3 5

Problem

  1. 删除单元格后,collectionView的IndexPath不会更新
  2. 崩溃应用程序在wishlistJSON.remove(at: indexPath.row)因为数组indexOutOfBound
ios swift swift4 collectionview
2个回答
0
投票

当细胞被重用时,将再次添加cell.wishButtonTapped动作!那不是我们想要的。

Protocols Approach solve this

protocol WishlistDelegate: class {
    func wishlistBtnTap(_ sender: WishCell)
}

WishCell

class WishCell: UICollectionViewCell {

    weak var delegate: WishlistDelegate?

    @IBAction func wishBtnTapped(_ sender: Any) {
        delegate?.wishlistBtnTap(self)
    }

CollectionView 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])
            cell.delegate = self

            return cell

        } else {
            return UICollectionViewCell()
        }
    }

extension - WishlistDelegate

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])
    }
}

0
投票

重新加载Collection-view必须要做到这一点

[self.collectionView performBatchUpdates:^{
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} 
© www.soinside.com 2019 - 2024. All rights reserved.