UISearchController 在 indexPath 上崩溃
UISearchController crash on indexPath
抱歉,这种问题我基本上是在请别人调试我的代码,但我已经坚持了一天多都无济于事。
问题是:我的 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 个结果,这是正确的。
第一个字符串是"puppy-1"也是正确的。
如果我不搜索任何内容,collectionView returns 图像列表正确。
更多代码在这里:
#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;
谢谢。
编辑:
崩溃日志:
您是否检查过在崩溃前从您的 numberOfItemsInSection 中得到了什么?如果你的 galleryButton 被选中并且你的 searchController 处于活动状态,你的 numberOfItemsInSection 和 cellForRowAtIndexPath 的设置方式你将获得许多基于 photoFileNames.count 的项目并且你崩溃的地方你正在检查搜索结果数组中的对象但是你的 indexPath.row 将超过该数字,因为您将根据 photoFileNames.count
获得许多单元格
抱歉,这种问题我基本上是在请别人调试我的代码,但我已经坚持了一天多都无济于事。
问题是:我的 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 个结果,这是正确的。
第一个字符串是"puppy-1"也是正确的。
如果我不搜索任何内容,collectionView returns 图像列表正确。
更多代码在这里:
#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;
谢谢。
编辑:
崩溃日志:
您是否检查过在崩溃前从您的 numberOfItemsInSection 中得到了什么?如果你的 galleryButton 被选中并且你的 searchController 处于活动状态,你的 numberOfItemsInSection 和 cellForRowAtIndexPath 的设置方式你将获得许多基于 photoFileNames.count 的项目并且你崩溃的地方你正在检查搜索结果数组中的对象但是你的 indexPath.row 将超过该数字,因为您将根据 photoFileNames.count
获得许多单元格