0

我在尝试使用 aUISearchBar从填充 a 的数组中过滤对象时遇到错误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
4

2 回答 2

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
于 2013-09-17T23:45:15.133 回答
1

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

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

    return [initialCities count]; /// All items
}
于 2013-09-17T23:38:34.703 回答