Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload section from collectionview

I tried to delete a cell from collectionview on didSelect method.

Deleting the data is working well.

But I'm getting this:

reason: 'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

This is delete cell function:

func deleteMovie(cell: MoiveCollectionViewCell) {
var indexPath: NSIndexPath = self.collectionView!.indexPathForCell(cell)!
 // remove and reload data
 self.collectionView.deleteItemsAtIndexPaths([indexPath])
 self.collectionView.reloadSections(NSIndexSet(index: indexPath.section))
}

and numberOfSectionsInCollectionView func :

 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //number_per_section = 3 
    if manager.movie_List.count % number_per_section  == 0
    {
        return manager.movie_List.count / number_per_section
     }
    else {  
       return manager.movie_List.count / number_per_section  + 1
         }
}

I just want to reload number of sections. What should I add?

like image 887
H.jenny Avatar asked Dec 15 '22 04:12

H.jenny


2 Answers

You need to update the datasource first before you make the calls to:

 collectionView.deleteItemsAtIndexPaths([indexPath])
 collectionView.reloadSections(NSIndexSet(index: indexPath.section))

So first delete the object in your datasource model (array, dictionary etc).

Then you can just do this:

 let indexSet = IndexSet(integer: indexPath.section)
 collectionView.reloadSections(indexSet)          
like image 104
Edward Avatar answered Jan 16 '23 04:01

Edward


The problem is you are just removing a cell from the section and that will definitely not work.

Once you have removed your cell you need to notify your manager.movie_List array too. So, remove the selectedIndex data from array and then try it out!

like image 23
Sohil R. Memon Avatar answered Jan 16 '23 04:01

Sohil R. Memon