带范围按钮的UISearchBar NSInternalInconsistencyException取消时崩溃

时间:2013-11-01 18:03:34

标签: iphone objective-c xcode core-data

我在这里有点亏。

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

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时,在范围按钮之间来回点击并执行几次搜索,我就可以关闭没问题了。

非常感谢核心数据专家提供的任何帮助。

0 个答案:

没有答案