I have a UISearchDisplayController which is behaving strangely under iOS7. The first time I do a search, everything is ok and the UISearchResultsTableView displays the results correctly.
The problem is that the second time I do a search (after dismissing the table), a blank line (looks like a cell) is added to the top of the search results table :
Each subsequent search then adds in another blank line to the top of the table, and so on and so on.
Here is the code :
//Setup the search bar.
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 360.0f, 44.0f)];
UIBarButtonItem * searchBarButton = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
searchBar.delegate = self;
controller = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
controller.searchResultsDataSource = self;
controller.searchResultsDelegate = self;
self.tabBarController.navigationItem.rightBarButtonItems = @[searchBarButton];
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
if (isLoading) {
return;
}
[self filter : searchText];
[controller.searchResultsTableView reloadData];
}
- (void)filter : (NSString *)text {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name CONTAINS[cd] %@ OR number CONTAINS[cd] %@)", text, text];
searchResults = [standArray filteredArrayUsingPredicate:predicate];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [searchResults count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if ([searchResults count] ==0) {
cell.textLabel.text = @"";
}
else {
NSString * name = [[searchResults objectAtIndex:indexPath.row] name];
NSString * number = [[searchResults objectAtIndex:indexPath.row] number];
NSString * resultsString = [NSString stringWithFormat:@"%@ %@", name, number];
cell.textLabel.text = resultsString;
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * name = [[searchResults objectAtIndex:indexPath.row] name];
Object * exhibitor = [self getExhibitorForStandName:standName];
if ([exhibitor.companyName isEqualToString:standName]) {
NSString * status = [[self getExhibitorForStandName:standName] status];
cell.backgroundColor = [standColourMapping objectForKey:status];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.searchDisplayController setActive:NO animated:YES];
...
}
}
Ok for future travellers, this looks like a weird bug whereby the height of the UISearchBar is added to the top of the searchResultsTable each subsequent time it appears.
The solution is the following :
controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(0.0f, 0.f, 0.f, 0.f); //Fix for weird weird iOS7 bug where each subsequent
//search adds the height of the searchBar to the top of the searchResultsTableView.