I'm trying to delete a collectionView cell using this code :
myDataSourceArray.remove(at:index)
collectionView.performBatchUpdates({
collectionView.deleteItems(at: [indexPath])
}
, completion: nil)
its very straight forward deletion ,and after trying to delete any cell from section 1 for example , it causes this exception :
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 1. The number of items contained in an existing section after the update (2) must be equal to the number of items contained in that section before the update (2), 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).'
my collectionView distribution for cells is each section contains 2 cells like image below

and for last section if its containing 1 cell , it will contain 1 cell
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
if myDataSourceArray.count%2==1 && myDataSourceArray.count/2 == section {
return 1
}else{
return 2
}
}
What exactly causes this exception ? Thanks in advance :)