Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewCell - how to select all items/cells in UIButton action method

I'm trying to select all UICollectionViewCells after a UIButton is tapped.

How do I do this?

like image 985
Sandeep Sachan Avatar asked Dec 30 '25 23:12

Sandeep Sachan


2 Answers

Updated for Swift 3

Just Shadow's Answer updated to Swift 3

  for i in 0..<assetCollectionView.numberOfSections {
    for j in 0..<assetCollectionView.numberOfItems(inSection: i) {
        assetCollectionView.selectItem(atIndexPath: IndexPath(row: j, section: i), animated: false, scrollPosition: .none)
    }
}
like image 94
Indrajit Sinh Rayjada Avatar answered Jan 01 '26 15:01

Indrajit Sinh Rayjada


You can select all cells in the first section through:

for (NSInteger row = 0; row < [self.collectionView numberOfItemsInSection:0]; row++) {
    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}

If you have more than 1 section, just use another nested for loop to loop through all the sections.

like image 28
Gasper Avatar answered Jan 01 '26 14:01

Gasper