【发布时间】:2015-01-28 11:11:39
【问题描述】:
UISearchDisplayController 有问题。我使用Storyboard 向UIViewController 添加了一个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 框架在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}>
【问题讨论】:
-
你能告诉我你在 UISearchBarDelegates 和 UISearchDislplayDelegates 方法中编写的代码
-
我刚刚添加了一个代码。
标签: ios cocoa-touch uitableview uisearchbar uisearchdisplaycontroller