sallyp
sallyp

Reputation: 226

UISearchController crash on indexPath

Sorry this is the kind of question which I'm basically asking someone to debug my code, but I've been stuck on it for more than a day with no avail.

The problem is: My UISearchController implementations do not work, and I believe it's stuck on indexPath.row.

Here is some code:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SSPhotosCollectionViewCell *cell;
    NSString *string;
    if (self.galleryButton.selected) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        if ([self.searchController isActive] && (![self.searchController.searchBar.text isEqualToString:@""])) {
            NSArray *results = [self.searchResults mutableCopy];
            string = [results objectAtIndex:indexPath.row];
        } else {
           string = [self.photoFileNames objectAtIndex:indexPath.row];
        }
        cell.imageView.image = [UIImage imageNamed:string];
    }
    else if (self.albumsButton.selected) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:albumIdentifer forIndexPath:indexPath];
        cell.imageView.image = [UIImage imageNamed:@"AlbumIcon.png"];
    }
    return cell;
}

Here is the line that crashs:

string = [results objectAtIndex:indexPath.row];

I just made the *results array to see what's actually in my searchResults array, and sure enough, it's filtering properly. I have a NSMutableArray containing these images:

self.photoFileNames = [NSMutableArray arrayWithObjects:@"puppy-1", @"puppy-2", @"puppy-3", @"puppy-4", @"puppy-5", @"puppy-6", @"puppy-7", @"puppy-8", @"puppy-9", @"puppy-10", @"puppy-11", @"puppy-12", nil];

And I searched for the string "1", I get 4 results back, which is correct.

enter image description here

The first string is "puppy-1" which is correct as well.

enter image description here

If I do not search for anything, the collectionView returns the list of images correctly.

More code here:

#pragma mark - UISearchControllerDelegate

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = searchController.searchBar.text;
    NSLog(@"%@", searchString);
    if (searchString == nil) {
        self.searchResults = [self.photoFileNames mutableCopy];
    } else {
        self.searchResults = [[NSMutableArray alloc] init];

        for (NSString *name in self.photoFileNames) {
            if ([name.lowercaseString containsString:searchString.lowercaseString]) {
                [self.searchResults addObject:name];
            }
        }
    }
    [self.collectionView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
    [self updateSearchResultsForSearchController:self.searchController];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.galleryButton.selected) {
        return self.photoFileNames.count;
    }
    else if (self.albumsButton.selected) {
        return 7;
    } else if (self.searchController.active) {
        return self.searchResults.count;
    }
    return 0;
}
// in viewDidLoad
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.scopeButtonTitles = @[];
self.collectionView.contentOffset =  CGPointMake(0, CGRectGetHeight(self.searchController.searchBar.frame));
[self.collectionView addSubview:self.searchController.searchBar];
self.definesPresentationContext = YES;

Thank you.

EDIT:

Crash log: enter image description here enter image description here

Upvotes: 0

Views: 116

Answers (1)

zfetters
zfetters

Reputation: 457

Have you checked what you are getting from you numberOfItemsInSection before the crash? The way your numberOfItemsInSection and cellForRowAtIndexPath are setup if your galleryButton is selected and your searchController is active you will get a number of items based on photoFileNames.count and where you are crashing you are checking for objects in the search results array but your indexPath.row would be going past that number because you are getting a number of cells based on photoFileNames.count

Upvotes: 1

Related Questions