1

我在这里有点不知所措。

我有一个带有两个范围按钮选项 A 和选项 B 的 UISearchBar。搜索在两个范围按钮上都可以正常工作,但是当我在搜索栏中单击取消并选择第二个范围按钮时,应用程序有时会因以下错误而崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 3 in section at index 0'

这是我的 UITableView 的相关代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSArray *results = [fetchedResultsController fetchedObjects];
    return [[fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (tableView == self.tableView && [[fetchedResultsController sections] count] > section) {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo name];
    }
    return nil;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSArray *alphabet = [NSArray arrayWithObjects:UITableViewIndexSearch, @"A", @"B", @"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil];
    if (tableView == self.tableView) {
        return alphabet;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if(tableView == self.tableView && [title isEqual:UITableViewIndexSearch]){
        [tableView scrollRectToVisible:[[tableView tableHeaderView] bounds] animated:NO];
        return -1;
    } else if (tableView == self.tableView) {
        return [self.fetchedResultsController.sectionIndexTitles indexOfObject:title];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"OptionACell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSManagedObject *category = [fetchedResultsController objectAtIndexPath:indexPath];
    if (self.searchBar.selectedScopeButtonIndex == CategoriesViewSearchScopeOptionB && ![self.searchBar.text isEqual:@""]) {
        [cell.textLabel setText:[category valueForKey:@"text"]];
    } else {
        [cell.textLabel setText:[category valueForKey:@"name"]];
    }
    return cell;
}

和 UISearchBar:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if([self.searchBar.text isEqual: @""] && self.searchBar.selectedScopeButtonIndex == CategoriesViewSearchScopeOptionB) {
        self.searchBar.text=@"";
        [self updateOptionAFetchRequest];
    } else {
        [self updateFetchRequest];
        NSFetchedResultsController *fetchController = [self fetchedResultsController];
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
    [self logSearchTerm:self.searchBar.text];
    self.searchBar.text = @"";
    self.searchBar.selectedScopeButtonIndex = CategoriesViewSearchScopeOptionA;
    [self updateOptionAFetchRequest];

    self.searchDisplayController.delegate = nil;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    [self logSearchTerm:self.searchBar.text];
    if([self.searchBar.text isEqual: @""]) self.searchBar.selectedScopeButtonIndex = CategoriesViewSearchScopeOptionA;
    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
    if([self.searchBar.text length] > 0) {
        [self updateFetchRequest];
    }
}

- (void)updateFetchRequest {
if (self.searchBar.selectedScopeButtonIndex == CategoriesViewSearchScopeOptionB) {
    [self updateOptionBFetchRequest];
    return;
}
[self updateOptionAFetchRequest];
}

我无法辨别崩溃的确切模式。每当我执行打开 UISearchBar 的操作时,它似乎总是崩溃,单击 OptionB 然后取消。但是当我打开 SearchBar,在范围按钮之间来回单击并执行几个搜索时,我可以关闭没有问题。

Core Data 专家的任何帮助将不胜感激。

4

0 回答 0