2016-06-10 46 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