[英]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
可能是您忘記了重新加載表格視圖
[friendslisttable reloadData];
在tableView:cellForRowAtIndexPath:
為什么要這樣做-> friendslisttable = nil
? 您做錯的另一件事是為您的friendslisttable
分配了一個NSString
類型值,它實際上是一個UITableView
實例。 這就是為什么出現此錯誤'NSInvalidArgumentException', reason: '-[__NSCFConstantString reloadData]: unrecognized selector sent to instance
。
我建議您應該遵循本教程,以與搜索控制器的概念保持一致。 http://useyourloaf.com/blog/updating-to-the-ios-8-search-controller.html
您需要什么:
創建像您已經在viewDidLoad
完成的搜索欄,並將searchBar
委托添加為self(也已完成)
使用<UISearchBarDelegate>
從委托實現一些方法
喜歡
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
在這種方法中,您應該過濾自己的dataSource並在tableView
重新加載數據。
如果一切正確完成-您將獲得預期的行為
關於您的代碼:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
永遠不會被調用-嘗試將breakPoint放入其中-這是原因之一
另一個問題-您的謂詞@"name contains %@"
–這意味着您在數組中具有屬性name
對象。 實際上,您沒有任何自定義對象-僅@"SELF contains[c] %@"
字符串,因此應改為@"SELF contains[c] %@"
如果您更新它,您將獲得正確的搜索結果:
下一個問題if (tableView == self.searchDisplayController.searchResultsTableView)
配置if (tableView == self.searchDisplayController.searchResultsTableView)
-永遠不會true
-如果您要使用它-您應該將基類更改為UISearchDisplayController
因此,如果您更正此問題-您將獲得可行的搜索解決方案。
我也建議你先閱讀羅伯特·馬丁的《清潔代碼》-ISBN-13:978-0132350884
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.