【问题标题】:How to delete multiple items in Collection View如何在集合视图中删除多个项目
【发布时间】:2016-06-10 02:36:58
【问题描述】:

我有 2 个这样的集合视图:

第一个是相册的收藏视图,第二个是每个相册中照片的收藏视图。

数据来自服务器。我想将一些照片从专辑 A 移动到专辑 B。

我试过这样:

if response != nil {
    // Update UI Move photo from AlbumA to AlbumB
    // Get all images from albumA
    var sourceImages: [UIImage] = []
    for i in 0..<self.checkSelectedImages[self.selectedAlbumIndex].count {
        if self.checkSelectedImages[self.selectedAlbumIndex][i] == false {
            let cell = self.imageCollectionView.cellForItemAtIndexPath(NSIndexPath(forRow: i, inSection: 0)) as! ODIProfileMovePhotoImageCell
            let sourceImage = cell.imageView.image
            self.imageCollectionView.deleteItemsAtIndexPaths([NSIndexPath(forRow: i, inSection: 0)])
            sourceImages.append(sourceImage!)
            self.imageCollectionView.reloadData()
        }
    }
    // Send those above images to AlBumB
    for i in 0..<sourceImages.count {
        self.imageCollectionView.insertItemsAtIndexPaths([NSIndexPath(forRow: i, inSection: 0)])
    }
    self.imageCollectionView.reloadData()
}

和 numberOfItemsInSection:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.albumCollectionView {
        return listData.count + 1
    }
    else if collectionView == self.imageCollectionView {
        if listData.count > 0 {
            if selectedAlbumIndex > 0 {
                return listData[selectedAlbumIndex - 1].photos.count
            } else {
                return thumbnailImagesFromPhotoLibrary.count
            }
        }
    }
    return thumbnailImagesFromPhotoLibrary.count
}

但它在这一行崩溃:

self.imageCollectionView.deleteItemsAtIndexPaths(indexPaths!)

上面写着:

由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 第 0 节中的项目数。包含在第 0 节中的项目数 更新后的现有节 (14) 必须等于 更新前该部分中包含的项目 (14),加号或减号 从该部分插入或删除的项目数(0 插入, 1 已删除)加上或减去移入或移出的项目数 该部分(0 移入,0 移出)。'

如何解决这个问题?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    您应该先从数组中删除元素,然后从UICollectionView 中删除。

    如果您使用checkSelectedImages 计算部分或部分中的项目数,请先从中删除元素。

    【讨论】:

    • 嗨@Eman Rezk。首先,感谢您的回答,但我只使用一个数组作为用于两个集合视图的数据源,因此很难再次更新数据源。而checkSelectedImages 不用于计算section 中的项目数。
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多