Reputation: 11702
I have 2 Collection Views like this:
The first one is the Collection View for Albums, the second one is the Collection View for photos in each Album.
The datas are from server. I want to move some photos from AlbumA to AlbumB.
I have tried like this:
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()
}
and 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
}
But it crashes at this line:
self.imageCollectionView.deleteItemsAtIndexPaths(indexPaths!)
It says:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (14) must be equal to the number of items contained in that section before the update (14), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
How to solve this problem?
Upvotes: 0
Views: 1074
Reputation: 422
You should remove the elements from the array first, then delete from the UICollectionView
.
If you use checkSelectedImages
to calculate the number of sections or items in a section, remove the element from it first.
Upvotes: 5