2016-04-01 56 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的單元格。