UISearchBar - 不显示搜索结果
UISearchBar - results of search not displayed
在我的项目中,我使用的是自定义 UISearchBar
。我完成了我的代码,但是当我尝试搜索任何内容时,它没有显示结果。
我正在分享一些代码片段。
.H文件
#import <UIKit/UIKit.h>
@interface FriendsViewController : UIViewController<UISearchDisplayDelegate , UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
{
UITableView *friendslisttable;
NSMutableArray *friendslist;
NSMutableArray *friendsnamearray;
NSMutableArray *profilepicarray;
UIImageView * frndprofpic;
UILabel *friendnamelabel;
NSArray *searchResults;
}
@end
我的.m文件
#import "FriendsViewController.h"
#import "ProfileViewController.h"
@interface FriendsViewController ()
@end
@implementation FriendsViewController
- (id)init
{
if (self = [super init])
{
self.title = @"Friends Request";
self.view.backgroundColor = [UIColor colorWithRed:100.0/255.0 green:125.0/255.0 blue:130.0/255.0 alpha:1];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Friends Request";
UISearchBar *searchh = [[UISearchBar alloc] init];
searchh.frame = CGRectMake(0 ,65, self.view.frame.size.width,45);
searchh.delegate = self;
searchh.showsBookmarkButton = NO;
searchh.placeholder = @"Search friend";
searchh.barTintColor = [UIColor whiteColor];
[self.view addSubview:searchh];
friendslisttable = [[UITableView alloc]initWithFrame:CGRectMake(0,110 , self.view.frame.size.width, self.view.frame.size.height)] ;
friendslisttable.delegate = self;
friendslisttable. dataSource = self;
[self.view addSubview:friendslisttable];
friendsnamearray = [[NSMutableArray alloc]initWithObjects:@"friend1",@"friend12",@"friend13",@"friend14",@"friend15",@"friend16",@"friend17",@"friend18",@"friend19",@"friend20",@"friend21 ",@"friend22",@"", nil];
profilepicarray = [[NSMutableArray alloc]initWithObjects:@"download3.jpeg", @"12045540_717548405011935_7183980263974928829_o.jpg" , @"download4.jpeg", @"download5.jpeg", @"download6.jpg", @"download12.jpeg", @"download13.jpeg", @"download16.jpeg",@"download3.jpeg", @"download6.jpg", @"download12.jpeg", @"download16.jpeg", @" ", nil];
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
searchResults = [friendsnamearray filteredArrayUsingPredicate:resultPredicate];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
#pragma mark - TableView Delegate Method -------------------->>>>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return searchResults.count;
}
else {
return friendsnamearray.count;
}
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellidentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidentifier];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
friendslisttable = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
friendslisttable = [searchResults objectAtIndex:indexPath.row];
} else
{
friendslisttable = [friendsnamearray objectAtIndex:indexPath.row];
}
frndprofpic = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];
frndprofpic.image=[UIImage imageNamed:[profilepicarray objectAtIndex:indexPath.row]];
[cell.contentView addSubview:frndprofpic];
friendnamelabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 250, 50)];
friendnamelabel.text = [friendsnamearray objectAtIndex:indexPath.row];
friendnamelabel.font = [UIFont fontWithName:@"ChalkboardSE-Regular" size:20];
[cell.contentView addSubview:friendnamelabel];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!indexPath.row) {
ProfileViewController *u = [[ProfileViewController alloc]init];
[self.navigationController pushViewController:u animated:YES];
}
}
- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor clearColor]];
tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}
@end
可能是你忘记重新加载你的 tableview
[friendslisttable reloadData];
在 tableView:cellForRowAtIndexPath:
你为什么要这样做 -> friendslisttable = nil
?
您做错的另一件事是您将 NSString
类型值分配给 friendslisttable
实际上是 UITableView
实例。这就是您收到此错误 'NSInvalidArgumentException', reason: '-[__NSCFConstantString reloadData]: unrecognized selector sent to instance
.
的原因
我建议您按照本教程学习搜索控制器的概念。
http://useyourloaf.com/blog/updating-to-the-ios-8-search-controller.html
你需要什么:
创建搜索栏,就像您在 viewDidLoad
中所做的那样,并为 searchBar
添加委托作为自己(也已完成)
使用<UISearchBarDelegate>
从委托中实现一些方法
喜欢
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
在此方法中,您应该过滤数据源并在 tableView
中重新加载数据。
如果一切正确 - 你会得到预期的行为
关于您的代码:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
将永远不会被调用 - 尝试将 breakPoint 放在里面 - 这是原因之一
另一个问题 - 是你的谓词 @"name contains %@"
- 这意味着你有数组中的对象 属性 name
。实际上你没有任何自定义对象 - 只有字符串,所以 @"SELF contains[c] %@"
应该是
如果你更新它,你将得到正确的搜索结果:
下一个问题 - tableViewCell 配置
if (tableView == self.searchDisplayController.searchResultsTableView)
- 永远不会 true
- 如果你想使用它 - 你应该将基础 class 更改为 UISearchDisplayController
因此,如果您更正此问题 - 您将获得可行的搜索解决方案。
此外,我建议您先阅读 Robert Martin "Clean Code" - ISBN-13: 978-0132350884
在我的项目中,我使用的是自定义 UISearchBar
。我完成了我的代码,但是当我尝试搜索任何内容时,它没有显示结果。
我正在分享一些代码片段。
.H文件
#import <UIKit/UIKit.h>
@interface FriendsViewController : UIViewController<UISearchDisplayDelegate , UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
{
UITableView *friendslisttable;
NSMutableArray *friendslist;
NSMutableArray *friendsnamearray;
NSMutableArray *profilepicarray;
UIImageView * frndprofpic;
UILabel *friendnamelabel;
NSArray *searchResults;
}
@end
我的.m文件
#import "FriendsViewController.h"
#import "ProfileViewController.h"
@interface FriendsViewController ()
@end
@implementation FriendsViewController
- (id)init
{
if (self = [super init])
{
self.title = @"Friends Request";
self.view.backgroundColor = [UIColor colorWithRed:100.0/255.0 green:125.0/255.0 blue:130.0/255.0 alpha:1];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Friends Request";
UISearchBar *searchh = [[UISearchBar alloc] init];
searchh.frame = CGRectMake(0 ,65, self.view.frame.size.width,45);
searchh.delegate = self;
searchh.showsBookmarkButton = NO;
searchh.placeholder = @"Search friend";
searchh.barTintColor = [UIColor whiteColor];
[self.view addSubview:searchh];
friendslisttable = [[UITableView alloc]initWithFrame:CGRectMake(0,110 , self.view.frame.size.width, self.view.frame.size.height)] ;
friendslisttable.delegate = self;
friendslisttable. dataSource = self;
[self.view addSubview:friendslisttable];
friendsnamearray = [[NSMutableArray alloc]initWithObjects:@"friend1",@"friend12",@"friend13",@"friend14",@"friend15",@"friend16",@"friend17",@"friend18",@"friend19",@"friend20",@"friend21 ",@"friend22",@"", nil];
profilepicarray = [[NSMutableArray alloc]initWithObjects:@"download3.jpeg", @"12045540_717548405011935_7183980263974928829_o.jpg" , @"download4.jpeg", @"download5.jpeg", @"download6.jpg", @"download12.jpeg", @"download13.jpeg", @"download16.jpeg",@"download3.jpeg", @"download6.jpg", @"download12.jpeg", @"download16.jpeg", @" ", nil];
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
searchResults = [friendsnamearray filteredArrayUsingPredicate:resultPredicate];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
#pragma mark - TableView Delegate Method -------------------->>>>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return searchResults.count;
}
else {
return friendsnamearray.count;
}
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellidentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidentifier];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
friendslisttable = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
friendslisttable = [searchResults objectAtIndex:indexPath.row];
} else
{
friendslisttable = [friendsnamearray objectAtIndex:indexPath.row];
}
frndprofpic = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];
frndprofpic.image=[UIImage imageNamed:[profilepicarray objectAtIndex:indexPath.row]];
[cell.contentView addSubview:frndprofpic];
friendnamelabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 250, 50)];
friendnamelabel.text = [friendsnamearray objectAtIndex:indexPath.row];
friendnamelabel.font = [UIFont fontWithName:@"ChalkboardSE-Regular" size:20];
[cell.contentView addSubview:friendnamelabel];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!indexPath.row) {
ProfileViewController *u = [[ProfileViewController alloc]init];
[self.navigationController pushViewController:u animated:YES];
}
}
- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor clearColor]];
tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}
@end
可能是你忘记重新加载你的 tableview
[friendslisttable reloadData];
在 tableView:cellForRowAtIndexPath:
你为什么要这样做 -> friendslisttable = nil
?
您做错的另一件事是您将 NSString
类型值分配给 friendslisttable
实际上是 UITableView
实例。这就是您收到此错误 'NSInvalidArgumentException', reason: '-[__NSCFConstantString reloadData]: unrecognized selector sent to instance
.
我建议您按照本教程学习搜索控制器的概念。 http://useyourloaf.com/blog/updating-to-the-ios-8-search-controller.html
你需要什么:
创建搜索栏,就像您在
viewDidLoad
中所做的那样,并为searchBar
添加委托作为自己(也已完成)使用
<UISearchBarDelegate>
从委托中实现一些方法
喜欢
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
在此方法中,您应该过滤数据源并在 tableView
中重新加载数据。
如果一切正确 - 你会得到预期的行为
关于您的代码:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
将永远不会被调用 - 尝试将 breakPoint 放在里面 - 这是原因之一
另一个问题 - 是你的谓词 @"name contains %@"
- 这意味着你有数组中的对象 属性 name
。实际上你没有任何自定义对象 - 只有字符串,所以 @"SELF contains[c] %@"
应该是
如果你更新它,你将得到正确的搜索结果:
下一个问题 - tableViewCell 配置
if (tableView == self.searchDisplayController.searchResultsTableView)
- 永远不会 true
- 如果你想使用它 - 你应该将基础 class 更改为 UISearchDisplayController
因此,如果您更正此问题 - 您将获得可行的搜索解决方案。
此外,我建议您先阅读 Robert Martin "Clean Code" - ISBN-13: 978-0132350884