UISearchBar语言 - 没有搜索文本时显示所有行



我正在使用带有两个范围按钮的UISearchBar。我想使用范围按钮来过滤行,即使没有搜索文本。现在,仅当有搜索文本时才过滤行。从调试来看,搜索结果表视图的帧大小为零。

有什么想法吗?

看看这个...

在 ViewController.h 文件中:

@interface ViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>
{
    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;
}
@property (nonatomic, retain) NSArray *friendsArray;
@property (nonatomic, retain) NSArray *searchResults;
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope;

在ViewController.m文件中:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.friendsArray = [[NSArray alloc]initWithObjects:@"Napoli",@"Juventus",@"Inter",@"Milan", @"Lazio",@"Real Madrid",@"Barcelona",@"Villareal",@"Valencia",@"Deportivo",@"Manchester City",@"Manchester United",@"Chelsea",@"Arsenal",@"Liverpool", nil];
        self.searchResults = [[NSArray alloc] init];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    self.tableView.tableHeaderView = searchBar;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchResults count];
    } else {
        return [self.friendsArray count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [self.friendsArray objectAtIndex:indexPath.row];
    }

    return cell;
}
#pragma mark - searchDisplayControllerDelegate

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];
    return YES;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];
    self.searchResults = [self.friendsArray filteredArrayUsingPredicate:resultPredicate];
}

为此,请保存一个包含所有数据的数组。在 textDidChange 中,当 searchText.length == 0 显示所有副本而不是搜索结果数组

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0) {
        tableSource = [NSMutableArray arrayWithArray: self.homeSheets.data];
        [self.tableView reloadData];
        return;
    }
    ...
}

最新更新