我有一个UISearchDisplayController,它在iOS7下表现得很奇怪。我第一次进行搜索时,一切正常,UISearchResultsTableView正确显示结果。
问题是我第二次进行搜索(在解除表格后),空白行(看起来像一个单元格)被添加到搜索结果表的顶部:
然后,每个后续搜索都会在表格的顶部添加另一个空白行,依此类推。
以下是代码:
//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];
...
}
}
答案 0 :(得分:5)
对于未来的旅行者来说,这看起来像一个奇怪的错误,UISearchBar的高度随后会出现在searchResultsTable的顶部。
解决方案如下:
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.