Question

I have a UICollectionView that I am using as a filter to allow users to visually select the categories they would like to browse. As they select (or deselect) those categories I am putting those category names into a a mutable array and storing them in NSUserDefaults.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

     NSString *selectedCategory = [categoryNames objectAtIndex:indexPath.row];
     [selectedCategories addObject:selectedCategory];

     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
     [userDefaults setObject:selectedCategories forKey:@"selectedCategory"];
     [userDefaults synchronize]; }


- (void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

     NSString *deSelectedCategory = [categoryNames objectAtIndex: indexPath.row];
     [selectedCategories removeObject:deSelectedCategory];

     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
     [userDefaults setObject:selectedCategories forKey:@"selectedCategory"];
     [userDefaults synchronize]; }

When a user returns back to the filters screen I would like to pre-populate those categories that they selected in the UICollectionView again as selected as they enter. I have been able to pull back the data stored in NSUserDefaults using the code below:

NSMutableArray *selectedCategories = [[userDefaults objectForKey:@"selectedCategory"]mutableCopy];

What I am having trouble figuring out is how to use the array I pull back from NSUserDefaults to compare to my categoryNames array (which lists out all the category names as NSStrings and is used to populate the UICollectionView in the cellForItematIndexPath) and find the corresponding indexpath in order to then pre-select the correct UICollectionView cells.

It seems that -indexofObject will be needed but I have been struggling to use it properly.

I am still fairly new to ios programming so any code examples would be greatly appreciated.

Was it helpful?

Solution

Presumably, you're loading the list of selected category names before any cells are displayed, such as in viewDidLoad. In that case, you would just need to add some logic to cellForRowAtIndexPath to select cells who's category name is in selectedCategories. It might look something like this:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = ...;

    NSString *category = self.categoryNames[indexPath.item];
    if ([self.selectedCategories containsObject:category] && !cell.selected) {
        [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        // There seems to be a bug in UICollectionView such that the previous
        // statement doesn't trigger the cell's selected view to be displayed.
        // Adding the following statement fixes the issue. Keeping the above
        // statement because it is important when allowsMultipleSelection is NO.
        cell.selected = YES;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top