2015-10-13 186 views
0

在我的項目中,我使用自定義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 
+0

您是否使用Xcode的6和iOS 8 ?如果你使用這個,UISearchBarDisplayController在iOS 8中被刪除。 – user3182143

+0

我試過你的編碼,甚至沒有調用搜索欄的委託方法。 – user3182143

回答

1

什麼u需要:

  1. 創建搜索欄像你已經做在viewDidLoadsearchBar添加委託作爲自我(也做)

  2. 使用<UISearchBarDelegate>

  3. 從代表實施幾種方法

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 

在該方法中ü應該過濾u'r DataSource和在tableView重新加載數據。

如果一切操作正確 - U將得到預期的行爲

關於你的代碼:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 

將不會被調用 - 儘量把斷點內 - 這就是原因

一個另一個問題 - 是你的謂詞@"name contains %@" - 這意味着你有對象與屬性name排列。Actualyü還沒有任何自定義對象 - 只有字符串,所以@"SELF contains[c] %@"應該代替

如果u更新U將得到正確的搜索結果:

enter image description here

下一個問題 - tableViewCell配置 if (tableView == self.searchDisplayController.searchResultsTableView) - 將永遠true - 如果你想使用它 - 你應該改變基類到UISearchDisplayController

所以,如果你糾正這 - 你會得到可行的解決方案進行搜索。

此外,我建議ü閱讀第一羅伯特·馬丁 「清潔守則」 - ISBN-13:978-0132350884


而且this postthis one可以幫助

+0

「乾淨的代碼」是一個很好的閱讀! –

1

可能是你忘了重新加載tableview中

[friendslisttable reloadData]; 
+0

由於未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSCFConstantString reloadData]:無法識別的選擇器發送到實例 –

+0

比您可以調試代碼可能b值不能正確傳遞到tableview中。 –

1

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