UISearchBar错误 - NSRangeException

时间:2013-09-17 22:24:10

标签: ios uitableview uisearchbar

我在尝试使用UISearchBar过滤来自填充UITableView的数组中的对象时遇到错误。错误说明:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'.

ViewController.m中的相关代码是:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle     reuseIdentifier:CellIdentifier];
    }
    if (isFiltered == YES)
    {
        NSLog (@"%i", indexPath.row);
        cell.textLabel.text = [filteredCities objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [initialCities objectAtIndex:indexPath.row];
    }
    return cell;
}

#pragma mark - UISearchBar Delegate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0)
    {
        isFiltered = NO;
    }
    else
    {
        isFiltered = YES;
        filteredCities = [[NSMutableArray alloc] init];

        for (NSString *cityName in initialCities)
        {
            NSRange cityNameRange = [cityName rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (cityNameRange.location != NSNotFound)
            {
                [filteredCities addObject:cityName];
            }
        }
    }
    [myTableView reloadData];
}

@end

2 个答案:

答案 0 :(得分:1)

您还应该将tableView:numberOfRowsInSection方法更改为以下内容:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (isFiltered) {
        return [filteredCities count]; /// Filtered items
    }

    return [initialCities count]; /// All items
}

答案 1 :(得分:1)

有一种更好的方法可以做到这一点。你应该使用搜索栏控制器。并使用这两种委托方法。

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF.property contains[cd] %@",
                                    searchText];

    self.searchResults = [self.datasource filteredArrayUsingPredicate:resultPredicate];
}

// code to refresh the table with the users search criteria
// this gets called everytime the user search string changes.
// also it calls the function above.
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

现在在您的表格视图中。你可以通过说

来引用搜索栏控制器创建的新表
self.searchBarController.searchResultsTable

你可以将它放在if else中并检查以查看表而不是使用isFiltered属性。

if (tableView == self.searchDisplayController.searchResultsTableView){
} else {   // your in the other original table