uisearchbar点击时下边有阴影原来是苹果自带的控件UISearchDisplayController

本文介绍如何使用 UISearchDisplayController 实现搜索功能,包括设置搜索栏、自定义搜索结果显示等,并提供完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://www.devdiv.com/uisearchdisplaycontroller_-blog-243602-50843.html

先把我已经实现的代码岾出来吧,不保证完全准确,但确实实现了:

注:头文件中要加上两个委托   <UISearchBarDelegate,UISearchDisplayDelegate>

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UISearchBar *searchBar = [[UISearchBaralloc]initWithFrame: CGRectMake(0,0,310, 40)];

    searchBar.placeholder=@"输入关键词!";

    searchBar.delegate =self;

    searchBar.keyboardType =UIKeyboardTypeDefault;

    searchBar.barStyle =UIBarStyleDefault;

    searchBar.backgroundColor = [UIColorlightGrayColor];

    

    UISearchDisplayController *displayCon = [[UISearchDisplayControlleralloc]initWithSearchBar:searchBarcontentsController:self];

    displayCon.delegate =self;

    displayCon.searchResultsDataSource =self;

    displayCon.searchResultsDelegate =self;

    

    

    for (UIView *subviewin displayCon.searchBar.subviews)

    {

        if ([subviewisKindOfClass:NSClassFromString(@"UISearchBarBackground")])

        {

            [subview removeFromSuperview];

            break;

        }

    }

    

    return [displayCon.searchBarautorelease];

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
2 {
3     return tableView==self.searchDisplayController.searchResultsTableView?0:22;
4 }

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

//    NSString *scope = [[self.searchDisplayController.searchBar scopeButtonTitles]

//                       objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]];

//    [self filterContentForSearchBarText:searchString scope:scope];

    return YES;

}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {

//    NSString *scope = [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption];

//    [self filterContentForSearchBarText:self.searchDisplayController.searchBar.text scope:scope];

    return YES;

}




其实这个应该是官方就有,这里只是记录给自个看看

其实应该比较好理解,UISearchDisplayController里面自带的tableView是继承我们本来创建的tableView,只是对数据进行更换,就能过显示出来了。
通过这样判断
1 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
2 {
3     return tableView==self.searchDisplayController.searchResultsTableView?0:22;
4 }
加入回调
1 UISearchDisplayDelegate

[代码]c#/cpp/oc代码:

1 UISearchDisplayController *m_searchDisplayController;
2   
3 m_searchDisplayController=[[UISearchDisplayController alloc] initWithSearchBar:m_ctable.m_searchbar.m_searchbar contentsController:self];//传入创建的tableVIew
4 m_searchDisplayController.delegate = self;
5 m_searchDisplayController.searchResultsDataSource = self;
6 m_searchDisplayController.searchResultsDelegate = self;

[代码]c#/cpp/oc代码:

01 #pragma mark -
02 #pragma mark Content Filtering
03  
04 - (void)filteredListContentForSearchText:(NSString*)searchText scope:(NSString*)scope
05 {
06     if(nil==m_filteredListContent)
07         m_filteredListContent=[NSMutableArray new];
08     [m_filteredListContent removeAllObjects];
09      
10     for(NSString* str in groups)
11     {
12         NSArray * contactSection = [contactTitles objectForKey:str];
13         for (NSMutableDictionary *eObj in contactSection)
14         {
15             if ([[[eObj objectForKey:@"name"] uppercaseString] rangeOfString:[searchText uppercaseString]].length>0)
16             {
17                 [m_filteredListContent addObject:eObj];
18             }
19         }
20     }
21 }
22  
23 #pragma mark -
24 #pragma mark UISearchDisplayController Delegate Methods
25  
26 - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
27      
28     UISearchBar *searchBar = self.searchDisplayController.searchBar;
29      
30     [searchBar setShowsCancelButton:YES animated:YES];
31      
32     for(UIView *subView in searchBar.subviews){
33          
34         if([subView isKindOfClass:UIButton.class]){
35              
36             [(UIButton*)subView setTitle:@"取消11111" forState:UIControlStateNormal];
37              
38         }
39          
40     }
41      
42 }
43  
44 //- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
45 //
46 //{
47 //   
48 ////    [self filterContentForSearchText:searchString];
49 ////   
50 ////    if ([filteredListPinYin count] == 0) {
51 //   
52 //        UITableView *tableView1 = self.searchDisplayController.searchResultsTableView;
53 //       
54 //        for( UIView *subview in tableView1.subviews ) {
55 //           
56 //            if( [subview class] == [UILabel class] ) {
57 //               
58 //                UILabel *lbl = (UILabel*)subview; // sv changed to subview.
59 //               
60 //                lbl.text = @"没有结果";
61 //                return YES;
62 //               
63 //            }
64 //           
65 //        }
66 //    return YES;
67 //}
68  
69 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
70 {
71     [self filteredListContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
72      
73     return YES;
74 }
75  
76 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
77 {
78     [self filteredListContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
79      
80     return YES;
81 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值