2016-04-01 59 views
0

对不起,这是一个问题,我基本上要求有人调试我的代码,但我一直坚持了一天以上,没有用。索引路径上的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; 
} 

这里是crashs行:

string = [results objectAtIndex:indexPath.row]; 

我刚才提出的*结果阵列,看看什么是真正的在我的SearchResult所阵列,果然,它正在过滤。我有一个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

第一字符串是 “小狗-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

+0

首先,请提一下崩溃日志说的。其次,在创建细胞的时候,如果 - 否则如果不是很好的做法。重写它,使其成为if-else。 – n00bProgrammer

+0

我已经添加了一个崩溃日志(我认为这是它是什么)我的歉意,我刚开始iOS编程,直到现在甚至不知道崩溃日志。感谢评论,我会尝试一下! – sallyp

+0

您是否在碰撞之前检查了您从numberOfItemsInSection获取的内容?你的numberOfItemsInSection和cellForRowAtIndexPath的设置方式,如果你的galleryButton被选中,并且你的searchController被激活,你会得到一些基于photoFileNames.count的项目,你会在崩溃的地方检查搜索结果数组中的对象,但是你的indexPath.row将会超过这个数字,因为你正在获取一些基于photoFileNames.count的细胞。 – zfetters

回答

1

你检查,你是从你numberOfItemsInSection在崩溃之前得到什么?你的numberOfItemsInSection和cellForRowAtIndexPath的设置方式,如果你的galleryButton被选中,并且你的searchController被激活,你会得到一些基于photoFileNames.count的项目,你会在崩溃的地方检查搜索结果数组中的对象,但是你的indexPath.row将会超过这个数字,因为你得到了一些基于photoFileNames.count的单元格。