i'm trying to add a UISearchDisplayController and UISearchBar to my UIViewController with UITableViewController add with InterfaceBuilder, this is my code:
@interface MyClass () <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate, UISearchDisplayDelegate>
{
UISearchBar *searchBar;
UISearchDisplayController *searchDisplayController;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation MyClass
- (void)viewDidLoad
{
UINib *myCellNib = [UINib nibWithNibName:@"MyCell_iPhone" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:myCellNib forCellReuseIdentifier:CellIdentifier];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
[self.tableView setRowHeight:84];
[self.tableView setSectionHeaderHeight:45];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -44 , 320, 44)];
searchBar.delegate = self;
[searchBar setBackgroundImage:[UIImage imageNamed:@"NavBar.png"]];
[self.view addSubview:searchBar];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
[searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern_bg"]]];
[searchDisplayController.searchResultsTableView setSeparatorColor:[UIColor clearColor]];
[searchDisplayController.searchResultsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[searchDisplayController.searchResultsTableView registerNib:myCellNib forCellReuseIdentifier:CellIdentifier];
[searchDisplayController.searchResultsTableView setRowHeight:84];
[searchDisplayController.searchResultsTableView setSectionHeaderHeight:0];
}
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredInfo removeAllObjects];
[self doLocalSearch:searchText];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell_iPhone *cell = (MyCell_iPhone *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (tableView == searchDisplayController.searchResultsTableView)
{
.....code to set information in cell
} else {
.....code to set information in cell
}
return cell;
}
My main UITableView works perfectly, instead the searchDisplayController.searchResultsTableView have this problem, the first time i make the search all go fine, i can see MyCustom Cell, i can see the background i insert in the uitableview, so all works, then i press the Cancel button to close the search i reopen the search view, i make another search and i receive this error:
*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:]
i have insert a lot of breakpoint to see where is the problem, and i found that here:
MyCell_iPhone *cell = (MyCell_iPhone *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
on the second search the value is nil, the table view lose it's background, and return the classical white background, and crash, what is the problem? i repeat my main uitableview works perfectly...
i have to insert the custom setting of the UISearchDisplayController table view in this delegate method:
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
[searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern_bg"]]];
[searchDisplayController.searchResultsTableView setSeparatorColor:[UIColor clearColor]];
[searchDisplayController.searchResultsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
UINib *myCellNib = [UINib nibWithNibName:@"MyCell_iPhone" bundle:[NSBundle mainBundle]];
[searchDisplayController.searchResultsTableView registerNib:myCellNib forCellReuseIdentifier:CellIdentifier];
[searchDisplayController.searchResultsTableView setRowHeight:84];
[searchDisplayController.searchResultsTableView setSectionHeaderHeight:0];
}