我对UISearchDisplayController
有疑问。我使用UIViewController
向Storyboard
添加了一个UITableView。 UITableViewDataSource
和UITableViewDelegate
已连接到Storyboard中的myViewController。
我在myViewController中实现了以下delegates
和datasources
:
UISearchBarDelegate
,UISearchDisplayDelegate
,UITableViewDataSource
,UITableViewDelegate
。
在表格视图中显示所有数据都可以正常工作,但是当我开始搜索时,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;
以下是故事板连接的简介:
screenshoot of storyboard connections
编辑3:
我刚在日志中发现UITableView's
方法中的CGRectZero
框架为numberOfRowsInSection
:
(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}>
答案 0 :(得分:1)
我可能有点偏离主题,但你应该知道这一点。
请勿使用UISearchDisplayController
因为它不再稳定。
Apple文档
重要提示:在iOS 8中不推荐使用UISearchDisplayController UISearchDisplayDelegate也被弃用了。)管理 在iOS 8和iOS中显示搜索栏并显示搜索结果 之后,改为使用UISearchController。
答案 1 :(得分:0)
您是否检查过numberOfRowsInSection返回的内容。在filterContentForSearchText方法执行shouldReloadTableForSearchString方法之后,还要检查_filteredProducts数组大小。将NSLog放在每个方法中并调试代码。
尝试使用此NSPredicate格式字符串:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like[cd] %@", searchText];
答案 2 :(得分:0)
您是否已将班级或自定义单元格笔记注册到UISearchDisplayController
表?
我使用UISearchDisplayController
委托方法:
- (void)searchDisplayController:(UISearchDisplayController *)controller
didLoadSearchResultsTableView:(UITableView *)tableView
{
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:CellIdentifier];
}