在我的項目中,我使用自定義UISearchBar
。我完成了我的代碼,但是當我嘗試搜索任何內容時,它不顯示結果。 我正在分享一些代碼片段。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
您是否使用Xcode的6和iOS 8 ?如果你使用這個,UISearchBarDisplayController在iOS 8中被刪除。 – user3182143
我試過你的編碼,甚至沒有調用搜索欄的委託方法。 – user3182143