我对 UISearchDisplayController
有疑问。我使用 Storyboard
添加了一个 UITableView 到 UIViewController
。 UITableViewDataSource
和 UITableViewDelegate
连接到 Storyboard 中的 myViewController。
我在 myViewController 中实现了以下 delegates
和 datasources
:
UISearchBarDelegate
、UISearchDisplayDelegate
、UITableViewDataSource
、UITableViewDelegate
。
在表格 View 中显示所有数据工作正常,但是当我开始搜索时,调用了 numberOfRowsInSection
: 方法,但之后 cellForRowAtIndexPath
: 没有被调用,并且 searchResultsTableView
根本不显示。
有人可以帮助我吗?
下面是UISearchDisplayDelegate
和UISearchBarDelegate
方法的代码。
编辑:
static NSString *ProductCellIdentifier = @"TKMProductTableCell";
@interface TKMSearchAllProductsTableVC () <UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate> {
UISearchDisplayController *searchDisplayController;
}
@property (nonatomic, strong) NSArray *allProducts;
@property (nonatomic, strong) NSMutableArray *filteredProducts;
@property (nonatomic, strong) UISearchBar *searchBar;
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == searchDisplayController.searchResultsTableView) {
return [_filteredProducts count];
} else {
return [_allProducts count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TKMProductTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ProductCellIdentifier];
// Configure the cell...
[self configureProductCell:cell atIndexPath:indexPath inTableView:tableView];
return cell;
}
- (void)configureProductCell:(TKMProductTableCell *)cell atIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
{
TKMProduct *product = nil;
if (tableView == searchDisplayController.searchResultsTableView) {
product = self.filteredProducts[indexPath.row];
} else {
product = self.allProducts[indexPath.row];
}
cell.lblTitle.text = product.name;
}
#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self.navigationItem setRightBarButtonItem:nil];
searchBar.showsCancelButton = YES;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
[searchDisplayController setActive:YES animated:YES];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
[searchBar resignFirstResponder];
[searchDisplayController setActive:NO animated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UISearchDisplayDelegate
- (void)filterContentForSearchText:(NSString *)searchText
{
[self.filteredProducts removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
_filteredProducts = [NSMutableArray arrayWithArray:[_allProducts filteredArrayUsingPredicate:predicate]];
}
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
tableView.backgroundColor = [UIColor redColor];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if (![searchString isEqualToString:@""] && searchString != nil) {
[self filterContentForSearchText:searchString];
[searchDisplayController.searchResultsTableView setHidden:NO];
}
return YES;
}
编辑 2
这是我的 UISearchDisplayController 代码:
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
这是 Storyboard连接的屏幕截图:
screenshoot of storyboard connections
编辑 3:
我刚刚在日志中发现 UITableView 的
框架是numberOfRowsInSection
方法中的CGRectZero
:
(lldb) po [searchDisplayController searchResultsTableView]
<UISearchResultsTableView: 0x1368c4a00; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1744420d0>; layer = <CALayer: 0x174821e20>; contentOffset: {0, 0}; contentSize: {0, 0}>
(lldb) po [self.searchDisplayController searchResultsTableView]
<UISearchResultsTableView: 0x1368c4a00; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1744420d0>; layer = <CALayer: 0x174821e20>; contentOffset: {0, 0}; contentSize: {0, 0}>
(lldb) po self.tableView
<UITableView: 0x136878e00; frame = (0 0; 320 504); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x17444dd70>; layer = <CALayer: 0x17422e300>; contentOffset: {0, 0}; contentSize: {320, 117906}>
(lldb) po tableView
<UISearchResultsTableView: 0x1368c4a00; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1744420d0>; layer = <CALayer: 0x174821e20>; contentOffset: {0, 0}; contentSize: {0, 0}>
最佳答案
建议
我可能有点跑题了,但你应该知道这一点。
不要使用 UISearchDisplayController
因为它不再稳定。
Apple 文档
Important: UISearchDisplayController is deprecated in iOS 8. (Note that UISearchDisplayDelegate is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead use UISearchController.
关于iOS UISearchDisplayController 不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190763/