申し訳ありませんが、これは基本的に誰かに私のコードをデバッグするよう依頼している質問ですが、無駄な1日以上それに取り組んでいます。UISearchControllerが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;
}
はcrashsラインです:
string = [results objectAtIndex:indexPath.row];
私はちょうど私に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件の結果を得ます。
最初の文字列は "子犬-1" も正確です。
私が何を検索していない場合は、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;
ありがとうございます。
EDIT:
まず、クラッシュログに何が言及されているかをご記入ください。 第2に、セルを作成するときに、if-elseは良い習慣ではありません。 if-elseになるように書き直してください。 – n00bProgrammer
クラッシュログを追加しました(私はそうだと思います)。申し訳ありません、私はちょうどiOSプログラミングを開始しましたが、今までクラッシュログについて知りませんでした。コメントありがとう、私はそれを試してみよう! – sallyp
クラッシュ前にnumberOfItemsInSectionから取得しているものを確認しましたか? galleryButtonが選択されていてsearchControllerがアクティブな場合、numberOfItemsInSectionとcellForRowAtIndexPathの設定方法は、photoFileNames.countに基づいていくつかの項目を取得し、検索結果配列内のオブジェクトをチェックしている間はクラッシュしますが、indexPath.rowあなたがphotoFileNames.countに基づいていくつかのセルを取得しているので、その番号を越えています。 – zfetters