Ios 搜索栏没有';不显示结果
我在UISearch酒吧练习。它应该显示它的作用域,但我不工作。我已将搜索栏视图控制器连接到原始视图控制器。 我检查了控制台(我放置了NSLog以查看NSPredicate属性是否有效),它会显示结果,但不会显示在屏幕上。 有什么建议吗。 谢谢:) #导入“ViewController.h” #导入“detailViewController.h”//I我意识到这不是一个好做法。第一个字母应该是大写的Ios 搜索栏没有';不显示结果,ios,objective-c,uisearchbar,Ios,Objective C,Uisearchbar,我在UISearch酒吧练习。它应该显示它的作用域,但我不工作。我已将搜索栏视图控制器连接到原始视图控制器。 我检查了控制台(我放置了NSLog以查看NSPredicate属性是否有效),它会显示结果,但不会显示在屏幕上。 有什么建议吗。 谢谢:) #导入“ViewController.h” #导入“detailViewController.h”//I我意识到这不是一个好做法。第一个字母应该是大写的 @interface ViewController () { NSMutableArra
@interface ViewController ()
{
NSMutableArray *_recipes;
NSString *_recipeName;
NSArray *_searchResults;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self;
self.tableView.dataSource = self;
_recipes = [NSMutableArray arrayWithObjects:@"Egg Benedict",@"Mushroom Risotto",@"Full Breakfast", @"Hamburger",@"Ham and Egg Sandwich", @"Cream Brelee",@"White Chocolate Donut",@"Starbucks Coffee",@"Vegetable Curry", @"Instant Noodle With Egg",@"Noodle with BBQ Pork", @"Japanese Noodle with Pork",@"Green Tea", @"Thai shripm Cake", @"Angry Birds Cake",@"Ham and Cheese Panini",nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
# pragma mark Dlegate method
// Data source protocol
- (NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section
{
if(self.tableView == self.searchDisplayController.searchResultsTableView)
{
return [_searchResults count];
}
else
{
return [_recipes count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(self.tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = _searchResults[indexPath.row];
}
else
{
cell.textLabel.text = _recipes[indexPath.row];
}
return cell;
}
// Delegate Protocol
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"row %i and section %i",indexPath.row,indexPath.section);
_recipeName = _recipes[indexPath.row];
[self performSegueWithIdentifier:@"CellSelectionSegue" sender:self];
}
# pragma mark Segue method
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"segue");
detailViewController *detailVC = segue.destinationViewController;
detailVC.string = _recipeName;
NSLog(@"%@",_recipeName);
}
# pragma mark Searchbar datasource
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
_searchResults = (NSArray *)[_recipes filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@",_searchResults);
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
@end
您是否为
SearchDisplayController
设置了代理
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;
编辑:
我将把我的评论编辑到这里。我相信你的推荐信
if (self.tableView == self.searchDisplayController.searchResultsTableView)
应该是
if (tableView == self.searchDisplayController.searchResultsTableView)
原因是
self。tableView
永远不会是SearchDisplayController
,因为在启动搜索时,它实际上会在原始的UITableViewController
上创建一个新的UITableViewController
。search
中还有一个委托方法。尝试将其添加到您的项目中。
代码如下:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:self.searchDisplayController.searchBar.text
scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
尝试将
\u searResults
记录在cellForRowAtIndexPath:
方法中,如果将if(self.tableView==
交换为if(tableView=
?避免self.tableView
),则只需使用if(tableView==self.search.searchResultsTableView)
。如果可行,试试看。@searchDisplayController提到的Toshi实际上在现有的tableview上创建了一个新的tableview。当您使用self.tableview
时,它总是指最初显示的原始tableview
。方法-(UITableViewCell*)tableview中的tableview
:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
实际上返回当前上下文中引用的tableView
。也就是说,当您使用搜索时,tableView
实际上引用了您的searchDisplayController
的tableView
。我希望这会清除它。