UISearchBar、UISearchDisplayController--(图)

来源:互联网 发布:商标设计软件下载 编辑:程序博客网 时间:2024/05/18 20:31

单纯性使用UISearchBar:



使用UISearchDisplayController之后:



#import "AZRootViewController.h"


@interfaceAZRootViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSArray *dataArray;

@property (nonatomic,strong)NSMutableArray *searchArray;

@property (nonatomic,strong)UISearchDisplayController *dc;

@end


@implementation AZRootViewController

-(UISearchDisplayController *)dc

{

   if (_dc) {

        _dc=[[UISearchDisplayControlleralloc] init];

    }

   return _dc;

}

-(UITableView *)tableView

{

   if (!_tableView) {

        _tableView=[[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];

    }

    return_tableView;

}

-(NSArray *)dataArray

{

    if (!_dataArray)

    {

    _dataArray=@[@"Apple",@"MicroSoft",@"Google",@"Sumaung",@"Tencet"];

    }

   return _dataArray;

}

-(NSArray *)searchArray

{

    if (!_searchArray) {

        _searchArray=[[NSMutableArrayalloc] init];

    }

    return_searchArray;

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    returnself;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    [self.viewaddSubview:self.tableView];

    self.tableView.dataSource=self;

    self.tableView.delegate=self;

    

    UISearchBar *searchBar=[[UISearchBaralloc] initWithFrame:CGRectMake(0,0,300,30)];

    [self.tableViewsetTableHeaderView:searchBar];

    

    //搜索结果显示控制器    

    _dc=[[UISearchDisplayControlleralloc] initWithSearchBar:searchBarcontentsController:self];

    

    _dc.searchResultsDataSource=self;

    _dc.searchResultsDelegate=self;

    

    

}

-(BOOL)prefersStatusBarHidden

{

    returnYES;

}

#pragma mark -- 搜索栏,单纯性使用

-(void)createSearchBar

{

    //UISearchBar -- 搜索栏

    

    UISearchBar *searchBar=[[UISearchBaralloc] initWithFrame:CGRectMake(10,20,300,88)];

    

    

   /*

     显示按键

     

     这些按键都是显示,并没有添加任何的响应事件。

     */

    

    //01显示取消按键

    searchBar.showsCancelButton=YES;

    //02显示书签按键

    searchBar.showsBookmarkButton=YES;

    //03显示搜索结果按键

    searchBar.showsSearchResultsButton=YES;

    //04 显示范围栏

    searchBar.showsScopeBar=YES;

    [searchBarsetScopeButtonTitles:@[@"新浪",@"百度",@"网易"]];

    

    

    //设置图片

    [searchBar setImage:[UIImageimageNamed:@"001.png"]forSearchBarIcon:UISearchBarIconSearchstate:UIControlStateNormal];

    

   /*

     为以上这些按键添加响应事件,使用代理,完成协议

     */

    

    //设置代理

    searchBar.delegate=self;

    [self.viewaddSubview:searchBar];


}


#pragma mark -- 绑定tableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    if ([tableViewisKindOfClass:[_dc.searchResultsTableViewclass]]) {

        returnself.searchArray.count;

    }

    returnself.dataArray.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   

   static NSString *idn=@"cell";

    UITableViewCell *cell=[self.tableViewdequeueReusableCellWithIdentifier:idn];

   if (!cell) {

        cell=[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:idn];

    }

    if ([tableViewisKindOfClass:[_dc.searchResultsTableViewclass]]) {

        cell.textLabel.text=self.searchArray[indexPath.row];

    }

   else

    {

        cell.textLabel.text=self.dataArray[indexPath.row];

    }

   return cell;


}



#pragma mark -- 实现searchBar中的协议

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar// called when keyboard search button pressed

{

    NSLog(@"点击搜索按钮");


}

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar// called when bookmark button pressed

{

     NSLog(@"点击书签按钮");

}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar// called when cancel button pressed

{

     NSLog(@"点击取消按钮");

}

- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBarNS_AVAILABLE_IOS(3_2)// called when search results button pressed

{

    NSLog(@"点击结果按钮");


}


- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScopeNS_AVAILABLE_IOS(3_0)

{

   if (selectedScope ==0) {

       NSLog(@"新浪");

    }

   if (selectedScope ==1) {

       NSLog(@"百度");

    }

   if(selectedScope==2)

    {

       NSLog(@"网易");

    }


}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


0 0
原创粉丝点击