0

我使用單個NSFetchedResultController來填充self.tableViewUISearchDisplayControler.tableView。使用搜索欄和貶如果我選擇在self.tableView任何行後,我得到這個錯誤:搜索後重新填充UITableView

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 

我想這地方我想念重裝數據,但我不知道在哪裏呢? 這裏有一些方法,我用了兩個填充看法於此項目表格:

#pragma mark - Table View 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Count sections in FRC 
    return [[self.groupsFRC sections] count]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Count groups in each section of FRC 
    return [[[self.groupsFRC sections] objectAtIndex:section] numberOfObjects]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Get reusable cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell"]; 

    // If there isn't any create new one 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GroupCell"]; 
    } 

    Group *group = [self.groupsFRC objectAtIndexPath:indexPath]; 

    cell.textLabel.text = group.name; 

    // Checking if group has been selected earlier 
    if ([self.selectedGroups containsObject:@{@"name" : group.name, @"id" : group.cashID}]) { 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    } else { 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 

    return cell; 
} 

// Adding checkmark to selected cell 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
    Group *group = [self.groupsFRC objectAtIndexPath:indexPath]; 

    // Checking if selected cell has accessory view set to checkmark and add group to selected groups array 
    if (selectedCell.accessoryType == UITableViewCellAccessoryNone) 
    { 
     selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [self.selectedGroups addObject:@{@"name" : group.name, @"id" : group.cashID}]; 
     NSLog(@"%@", self.selectedGroups); 
    } 
    else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) 
    { 
     selectedCell.accessoryType = UITableViewCellAccessoryNone; 
     [self.selectedGroups removeObject:@{@"name" : group.name, @"id" : group.cashID}]; 
     NSLog(@"%@", self.selectedGroups); 
    } 

    // Hiding selection with animation for nice and clean effect 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

#pragma mark - Filtering 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText]; 
    self.groupsFRC = [Group MR_fetchAllSortedBy:@"caseInsensitiveName" 
             ascending:YES 
            withPredicate:resultPredicate 
             groupBy:@"sectionLetter" 
             delegate:self]; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 
    return YES; 
} 

-(void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView 
{ 
    // Setting FRC to fetch original table view data 
    self.groupsFRC = [Group MR_fetchAllSortedBy:@"caseInsensitiveName" 
             ascending:YES 
            withPredicate:nil 
             groupBy:@"sectionLetter" 
             delegate:self]; 
} 
+0

你嘗試過在你的'searchDisplayController的末尾添加一個'[self.tableView reloadData]'電話:didHideSearchResultsTableView:'方法? – foggzilla

+0

是和類似的錯誤發生,但當我點擊「UISerchBar」中的「交叉」。 – cojoj

回答

0

歐凱,我終於得到了這個問題,但與跳躍UISearchDisplayDelegate和實施這只是使用UISearchBarDelegate。下面是代碼:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    if (searchText.length > 0) { 
     resultPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText]; 
    } else { 
     resultPredicate = nil; 
    } 

    [self refreshFRC]; 
} 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    resultPredicate = nil; 
    [self refreshFRC]; 
} 

- (void)refreshFRC { 
    if (resultPredicate != nil) { 
     self.groupsFRC = [Group MR_fetchAllSortedBy:@"caseInsensitiveName" 
              ascending:YES 
             withPredicate:resultPredicate 
              groupBy:@"sectionLetter" 
              delegate:self]; 
    } else { 
     self.groupsFRC = [Group MR_fetchAllSortedBy:@"caseInsensitiveName" 
              ascending:YES 
             withPredicate:nil 
              groupBy:@"sectionLetter" 
              delegate:self]; 
     [self.tableView reloadData]; 
    } 
} 
+0

無論如何,UISearchDisplayControllers現在已被棄用並由iOS8取代,所以這是一個好方法 – Heckscheibe