Haider Ahmed Haider Ahmed - 3 months ago 13x
Swift Question

i have an issue on select all in collectionviewCell

i have button this button is select all cells in collectionView it is work ok but the problem is when i select all cells it take huge memory i don't know why please can tell me how can i select all cells in collectionView without take huge memory

the following my code:

func selectAllCells() {

var i = 0
while i < self.collectionView.numberOfItemsInSection(0) {
// her i get the index path
let indexPath = NSIndexPath(forItem: i, inSection: 0)
self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: false)

self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast
self.dictionaryOfSelectItem.updateValue(i, forKey: i)
let cell = self.collectionView.cellForItemAtIndexPath(indexPath) as! AlbumImagesCollectionViewCell
cell.imageViewCheck.hidden = false
self.dictionaryOfSelectItem.updateValue(i, forKey: i)

i = i + 1


self.moveOutlet.enabled = true
self.exportOutlet.enabled = true
self.deleteOutlet.enabled = true


Thanks advance


Create a global variable called selectAll, somewhere before viewDidLoad()

Inside your selectAll() function, set the variable you just created to true.

self.selectAll = true;
//Now call your reload function for your collectionView. 

Now in your cellForItemAtIndexPath:

   if selectAll {
        cell.selected = true;
} else {
        cell.selected = false;

When the user deselects "Select All" button, or does something else, then set selectAll to false and reload the data again.

If you want to perform anything with every cell once they are all selected, do it in the cellForItemAtIndexPath function.