2012-04-13 68 views
2

我正在使用帶兩個範圍按鈕的UISearchBar。即使沒有搜索文本,我也想使用範圍按鈕來過濾行。現在只有在搜索文本時纔會過濾行。從調試中可以看出,searchResultsTableView的幀大小爲零。UISearchBar - 當沒有搜索文本時顯示所有行

任何想法?

+0

有沒有能夠找到它呢? – Arbel 2012-07-27 07:26:55

回答

0

檢查了這一點...

在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]; 
} 
0

這樣做,節省一個包含所有數據的數組。 在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; 
    } 

    ... 
}