Home > DeveloperSection > Forums > How to allow multiple selection in UICollectionView in iOS?
Steven Hill
Steven Hill

Total Post:10

Points:72
Posted on    December-23-2015 6:57 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 1183  View(s)
Rate this:

I am create an application like Photo album in iPhone.
Can any one tell me that how can I add functionality for multiple selection for specific section in UICollectionView?
I am using this code:

[self.collectionView setAllowsMultipleSelection:YES];

but, the above code will affects all sections.
Please suggest me any solution?



Tarun Kumar

Total Post:210

Points:1614
Posted on    December-23-2015 11:07 PM

The code I am providing here it will allow multiple selection only at section 1 and will allow only one cell selected at any other section:
For Example:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 1) return;
    NSArray<NSIndexPath*>* indexNo = collectionView.indexPathsForSelectedItems;
    for (int i = 0; i<indexNo.count; i++) {
        NSIndexPath* currentIndex = indexNo[i];
        if(![currentIndex isEqual:indexPath] && currentIndex.section != 1) {
            [collectionView deselectItemAtIndexPath:currentIndex animated:YES];
        }
    }
}

Don't want to miss updates? Please click the below button!

Follow MindStick