UISearchController在indexPath上崩溃

时间:2016-04-01 17:07:21

标签: ios objective-c uisearchcontroller

对不起,这是我基本上要求某人调试我的代码的问题,但是我已经被困在这一天超过一天没有用了。

问题是:我的UISearchController实现不起作用,我相信它已经停留在indexPath.row上。

以下是一些代码:

- (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;
}

以下是崩溃的行:

string = [results objectAtIndex:indexPath.row];

我刚刚制作了* results数组,以查看我的searchResults数组中实际存在的内容,果然,它已正确过滤。我有一个包含这些图像的NSMutableArray:

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];

我搜索了字符串" 1",我得到了4个结果,这是正确的。

enter image description here

第一个字符串是" puppy-1"这也是正确的。

enter image description here

如果我没有搜索任何内容,则collectionView会正确返回图像列表。

此处有更多代码:

#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;

谢谢。

修改

崩溃日志: enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

您是否在崩溃前检查了您从numberOfItemsInSection获得的内容?如果您的galleryButton被选中并且您的searchController处于活动状态,则会设置numberOfItemsInSection和cellForRowAtIndexPath的方式,您将获得基于photoFileNames.count的许多项目,并且您在崩溃的地方检查搜索结果数组中的对象但是您的indexPath.row会超过这个数字,因为你得到了一些基于photoFileNames.count的单元格