2016-06-10 52 views
0

我有2点收集意见是这样的:如何收集删除多个项目,查看

enter image description here

第一个是收集信息查看相册,第二个是收集信息查看每个相册的照片。

数据来自服务器。我想将一些照片从AlbumA移到AlbumB。

我已经试过这样:

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中的项目数。更新后的 现有部分中包含的项目数(14)必须等于 更新前该部分包含的项目(14),正负 插入或从该部分删除的项目数(0插入, 1已删除)并加上或减去移入或移出 该部分(0移入,0移出)。'

如何解决这个问题?

回答

5

您应该首先从阵列中删除元素,然后从UICollectionView中删除。

如果您使用checkSelectedImages来计算节中的节或项的数量,请首先从其中删除该元素。

+0

Hi @Eman Rezk。首先,感谢您的回答,但我只使用一个数组作为用于Collection Views的DataSource,因此很难再次更新DataSource。并且'checkSelectedImages'不用于计算节中项目的数量。 – Khuong

+0

如果这样做,您需要处理复制数组。 – Nick