gpt4 book ai didi

ios - 搜索栏不显示结果

转载 作者:行者123 更新时间:2023-11-28 22:16:41 24 4
gpt4 key购买 nike

enter image description here我在 UISearch 栏上练习。它应该显示它的范围,但我不起作用。我将搜索栏 View Controller 连接到原始 View Controller 。我检查了控制台(我放置了 NSLog 以查看 NSPredicate 属性是否有效),它显示了结果,但没有显示在屏幕上。请任何建议。谢谢:)

#import "ViewController.h" #import "detailViewController.h"//我意识到这不是好的做法。第一个字母要大写

@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 设置了委托(delegate)?

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原创。

关于ios - 搜索栏不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com