0

我有一個tableview,它是用fetchedresultscontrollersearchbar填充的。直到現在,在創建應用程序時,我只使用了7個單元格,並且工作得很好,但是自從我開始使用多於7個單元格時,當我觸摸搜索欄時,出現空白數組超出範圍的錯誤。但是當我將它返回到7個單元格時,它完美無缺。Tableview獲取索引超出空數組的邊界

什麼可能是錯的?

錯誤: 萌發過程中的應用程序由於未捕獲的異常「NSRangeException」,理由是:「*-[__NSArrayM objectAtIndex:]:指數2超出界限空陣」

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.title = @"Exercises"; 



    id appDelegate = [[UIApplication sharedApplication] delegate]; 
    _managedObjectContext = [appDelegate managedObjectContext]; 

    NSError *error; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     NSLog(@"%@ %@", error, [error userInfo]); 
     abort(); 
    } 

    _searchResults = [[NSMutableArray alloc] init]; 


} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    NSIndexPath *selectedIndexPath = [_tableView indexPathForSelectedRow]; 
    if (!selectedIndexPath) { 
     [_tableView setContentOffset:CGPointMake(0, 44) animated:NO]; 
    } 
    else { 
     [_tableView deselectRowAtIndexPath:selectedIndexPath animated:YES]; 
    } 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
    if (self.searchDisplayController.isActive) { 
     [self.searchDisplayController setActive:NO animated:YES]; 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - UITableViewDataSource 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (self.searchDisplayController.isActive) { 
     return 1; 
    } 
    return [[_fetchedResultsController sections] count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if (self.searchDisplayController.isActive) { 
     return nil; 
    } 
    return [[[_fetchedResultsController sections] objectAtIndex:section] name]; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 22)]; 
    view.backgroundColor = [UIColor colorWithRed:20.0/255.0 green:20.0/255.0 blue:20.0/255.0 alpha:0.8]; 

    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 160, 22)]; 
    sectionLabel.font = [UIFont fontWithName:@"Avenir-Book" size:16.0f]; 
    sectionLabel.text = @"Search Results"; 

    if (!self.searchDisplayController.isActive) { 
     sectionLabel.text = [[[_fetchedResultsController sections] objectAtIndex:section] name]; 
    } 

    sectionLabel.backgroundColor = [UIColor clearColor]; 
    sectionLabel.textColor = [UIColor whiteColor]; 
    [view addSubview:sectionLabel]; 
    return view; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (self.searchDisplayController.isActive) { 
     return [_searchResults count]; 
    } 
    return [[[_fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"exerciseCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     [self stampCell:cell atIndexPath:indexPath]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 


#pragma mark - Cell 

- (void)stampCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([cell.reuseIdentifier isEqualToString:@"exerciseCell"]) { 

     UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 2, 150, 22)]; 
     textLabel.tag = kExerciseCellTextLabel; 
     textLabel.font = [UIFont systemFontOfSize:18.0]; 
     textLabel.textColor = [UIColor blackColor]; 
     textLabel.highlightedTextColor = [UIColor whiteColor]; 
     textLabel.backgroundColor = [UIColor clearColor]; 
     [cell.contentView addSubview:textLabel]; 

     UILabel *detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 24, 150, 18)]; 
     detailTextLabel.tag = kExerciseCellDetailTextLabel; 
     detailTextLabel.font = [UIFont systemFontOfSize:14.0]; 
     detailTextLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]; 
     detailTextLabel.highlightedTextColor = [UIColor colorWithRed:127.0/255.0 green:127.0/255.0 blue:127.0/255.0 alpha:1.0]; 
     detailTextLabel.backgroundColor = [UIColor clearColor]; 
     [cell.contentView addSubview:detailTextLabel]; 

    } 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([cell.reuseIdentifier isEqualToString:@"exerciseCell"]) { 

     UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:kExerciseCellTextLabel]; 
     UILabel *detailTextLabel = (UILabel *)[cell.contentView viewWithTag:kExerciseCellDetailTextLabel]; 

     Exercise *exercise; 

     if (self.searchDisplayController.isActive) { 
      exercise = [_searchResults objectAtIndex:indexPath.row]; 
     } 
     else { 
      exercise = [_fetchedResultsController objectAtIndexPath:indexPath]; 
     } 

     textLabel.text = [exercise valueForKey:kExerciseName]; 
     detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", [[exercise valueForKey:kExerciseEType] valueForKey:kExerciseTypeName], [[exercise valueForKey:kExerciseGear] valueForKey:kGearName]]; 

     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    } 
} 


#pragma mark - UITableViewDelegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"exerciseDetail" sender:self]; 
} 

#pragma mark - UISearchBarDelegate 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [_searchResults removeAllObjects]; 
    if (searchString.length > 0) { 

     for (Exercise *exercise in [_fetchedResultsController fetchedObjects]) { 
      NSRange range = [[exercise valueForKey:kExerciseName] rangeOfString:searchString options:NSCaseInsensitiveSearch]; 
      if (range.location == 0) { 
       [_searchResults addObject:exercise]; 
      } 
     } 
    } 
    return YES; 
} 

- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar { 
    [super setPan]; 
    return YES; 
} 

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar 
{ 
    [super setPan]; 
    return YES; 
} 

#pragma mark - NSFetchedResultsControllerDelegate 

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (_fetchedResultsController) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:kExercise inManagedObjectContext:_managedObjectContext]; 
    [request setEntity:entity]; 

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:kExerciseName ascending:YES]; 
    [request setSortDescriptors:[NSArray arrayWithObject:sort]]; 

    NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:_managedObjectContext sectionNameKeyPath:kExerciseFirstLetter cacheName:@"ExerciseList"]; 
    [self setFetchedResultsController:fetchedResultsController]; 
    [_fetchedResultsController setDelegate:self]; 

    return _fetchedResultsController; 
} 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView beginUpdates]; 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{ 
    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath 
{ 
    UITableView *tableView = self.tableView; 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView endUpdates]; 
} 

@end 
+0

我編輯它,並添加了當觸摸搜索欄時觸發的錯誤。 – 2013-04-10 05:23:50

回答

1

這表明像一些陣列仍然empty..the更好的辦法數組元素後,檢查

-put的NSLog聲明和運行it..IT顯示了陣列保持爲空。

通過這樣做,如果你的問題沒有得到solved..please讓我know..so,我可以進一步幫助你..

+0

你檢查這個鏈接..它可能會幫助你..http://stackoverflow.com/questions/4965827/terminating-app-due-to-uncaught-exception-on-ios – Shivaay 2013-04-10 05:43:48

+0

哪裏將是一個有效的地方把nslog?或者是不相關的位置。 – 2013-04-10 05:48:54

+0

在視圖中快速枚舉是否與所有提取的對象一起加載,以檢查數組中是否有零,但都不爲零。這是你的意思嗎? – 2013-04-10 05:52:24

0

你在哪裏看到的崩潰 - 顯示時只是搜索結果還是顯示整個列表時?

如果它只是搜索結果,那麼你的_searchResults沒有超過7條記錄。

如果是當您顯示所有結果時,請檢查_fetchedResultsController.sections的每個值中的記錄數。看起來他們沒有超過7條記錄。

+0

如果我有8個或更多的單元格,並觸摸它的搜索欄崩潰,它甚至不顯示searchdisplaycontroller。它不必對searchResults做任何事情,當搜索欄被觸摸時,它甚至不會調用uitableviewdatasource。 – 2013-04-10 05:35:19

0

嗨,我猜UR去除_searchResults所有元素可能是由於這該應用程序崩潰。請檢查一次。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [_searchResults removeAllObjects]; 
+0

我應該刪除該行嗎? – 2013-04-10 15:22:25

相關問題