I implemented a searchbar in a table view. Every thing was ok. For customizing reasons I did embed both in a view controller. Since this point the App freeze after typing the first char into the search bar. Pausing/unpausing the app after that shows different points in debuger, so the app seems to be running and in the display I can see the char I typed highlited (bigger) on the keyboard. After playing around a bit I found out that, when I first time select the search bar, but do not type anything and hit cancel, this fixes the problem. After this I can use the search bar as expected.
I searched the internet for some days, but didn't found any hints to solve this problem. So any help is welcome.
here my code:
-(void)viewDidLoad
[super viewDidLoad];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PhaseHintergrund"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
self.searchItem = nil;
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"Suchleiste"]forState:UIControlStateNormal];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,44)];
searchBar.placeholder = @"Symptomsuche";
UIImage *myImage = [UIImage imageNamed: @"NavbarTransparent"];
searchBar.backgroundImage = myImage;
searchBar.delegate = self;
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchController.searchResultsTableView.separatorColor = self.tableView.separatorColor;
self.searchController.searchResultsTableView.backgroundView = tempImageView;
self.searchController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.tableView reloadData];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = nil;
cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITextView *symptom = (UITextView*) [cell viewWithTag:2];
symptom.text = (NSString*)[object valueForKey:@"name"];
-(BOOL)searchDisplayController:(UISearchDisplayController *)_controller shouldReloadTableForSearchString:(NSString *)searchString
self.searchItem = searchString;
self.fetchedResultsController = nil;
return YES;
-(void)searchDisplayController:(UISearchDisplayController *)_controller willUnloadSearchResultsTableView:(UITableView *)tableView
self.searchItem = nil;
self.fetchedResultsController = nil;
[self.tableView reloadData];
Edit Setting breakpoints in all UISearchDisplayController delegate methods, I found out that in the case of error (first time using the searchbar without cancel before) the method searchDisplayController: didLoadSearchResultsTableView: will not be executed. So it seems to me, that there is no active tableview at this time, which probably leads to the problem/error.
So the question is what I am missing or doing wrong in the initialisation, that canceling the search is fixing for me ?
Meanwhile I found a workaround for my problem, but still don't understand why it is working now. In viewDidLoad I added the following line after the initialization:
[self searchDisplayController:self.searchController didLoadSearchResultsTableView:self.searchController.searchResultsTableView];
Now the Searchbar is also working for the first search.