0

UISearchBara を使用して、 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

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 に答える
1

これを行うより良い方法があります。検索バー コントローラーを使用する必要があります。これら 2 つのデリゲート メソッドを使用します。

- (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

isFiltered プロパティを使用するのではなく、これを if else に入れてテーブルを確認することができます。

if (tableView == self.searchDisplayController.searchResultsTableView){
} else {   // your in the other original table
于 2013-09-17T23:45:15.133 に答える