搜索框的使用

来源:互联网 发布:windows性能监控脚本 编辑:程序博客网 时间:2024/05/20 21:19

//

//  ViewController.m

//  联系人管理

//

//  Created by 文华 on 15/12/15.

//  Copyright © 2015 文华. All rights reserved.

//


#import "ViewController.h"

#import "Contact.h"

#import "ShowViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutletUITableView *tableview;

//@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;

//@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchDisplay;

//@property (strong, nonatomic) IBOutlet UISearchController *searchDisplay;


@end


@implementation ViewController

//模态窗口直接打开就行,不需要函数

- (IBAction)addContact:(UIBarButtonItem *)sender {

    //*   模态窗口

//   EditViewController *editVC=[EditViewController new];

//   

//    [self presentViewController:editVC animated:YES completion:nil];

   

    

    

    


    

    //UIStoryboardSegue* segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"qq" source:self destination:editVC];

    //NSLog(@"%@",segue.identifier);

    

    //展示show窗口

    //[self performSegueWithIdentifier:@"add" sender:self];

    

}

#pragma mark 回传数据的代理方法

-(void)backWithContact:(Contact *)contactPsn IsEditing:(BOOL)isEditing

{

    

    if (!isEditing) {

       [self.contactsaddObject:contactPsn];

    }

    

    [self.tableviewreloadData];

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.contacts=[[NSMutableArrayalloc]init];

    self.searchResult=[[NSMutableArrayalloc]init];

    for (int i=0; i<3; i++) {

        Contact *contact=[Contactnew];

        contact.name=[NSStringstringWithFormat:@"person%d",i+1];

        contact.tel=[NSStringstringWithFormat:@"1850036%04d",arc4random_uniform(1000)];

        [self.contactsaddObject:contact];

        

    }

    

    

   

    //设置搜索框

    //Pass nil if you wish to display search results in the same view that you are searching.

    self.searchDisplay=[[UISearchControlleralloc]initWithSearchResultsController:nil];

    //该对象用来更新数据

    self.searchDisplay.searchResultsUpdater=self;

    //[self.searchDisplay.searchBar sizeToFit];

    //设置是否隐藏导航栏

    self.searchDisplay.hidesNavigationBarDuringPresentation=YES;

    //self.searchDisplay.searchBar.showsCancelButton=YES;

    CGRect frame=self.tableview.tableHeaderView.frame;

    frame.size.width=10;

    //self.tableview.tableHeaderView.frame=frame;

    //设置表格头部即为搜索框

    self.tableview.tableHeaderView=self.searchDisplay.searchBar;

    

    //self.searchDisplay.searchBar

    //显示取消按钮

    [self.searchDisplay.searchBarsetShowsCancelButton:YESanimated:NO];

    

    self.searchDisplay.dimsBackgroundDuringPresentation=false;

    

   

   

    self.tableview.delegate=self;

    self.tableview.dataSource=self;

    self.searchDisplay.delegate=self;

    //self.searchDisplay.delegate=self;

    //self.searchbar.delegate=self;

    //self.searchDisplay.searchResultsUpdater

   // NSLog(@"sss");

}

/*

UISearchControllersearchBar中的内容一旦发生变化,就会调用该方法-updateSearchResultsForSearchController.在其中, 我们可以使用NSPredicate来设置搜索过滤的条件.


更新UITableView

引入UISearchController之后, UITableView的内容也要做相应地变动:cell中要呈现的内容是allCities,还是filteredCities.

这一点, 可以通过UISearchControlleractive属性来判断,即判断输入框是否处于active状态.

UITableView相关的很多方法都要根据active来做判断:

 */

//考虑搜索结果

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

{

    if (!self.searchDisplay.active) {

        

        return self.contacts.count;

    } else {

        return self.searchResult.count;

    }

    

}


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

{

    static NSString *cellID=@"contactCell";

    //1,从重用池中找不用的cell对象

    UITableViewCell *cell=[self.tableviewdequeueReusableCellWithIdentifier:cellID];

    //2,如果没有救自己创建

    if (cell==nil) {

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellID];

        

    }

    Contact *contact=[Contactnew];

    //3,设置cell的属性

     if (!self.searchDisplay.active) {

         contact=self.contacts[indexPath.row];

     }

    else

    {

        contact=self.searchResult[indexPath.row];

    }

    //Contact *contact=self.contacts[indexPath.row];

    cell.textLabel.text=contact.name;

    cell.detailTextLabel.text=contact.tel;

    cell.detailTextLabel.textAlignment=NSTextAlignmentLeft;

    cell.detailTextLabel.textColor=[UIColorgrayColor];

    //NSLog(@"%@",contact.tel);

 

    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 40;

}

#pragma mark - delegate

#pragma mark 选中单元格时候的动作

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

//    if (tableView==self.tableview) {

//        self.IsSearchResult=NO;

//    }

//    else

//    {

//        self.IsSearchResult=YES;

//    }

    NSLog(@"222");

    [selfperformSegueWithIdentifier:@"show"sender:self];

    

}

#pragma mark segue跳转前的准备工作

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

{

    NSLog(@"连线=%@",segue.identifier);

    if ([segue.identifierisEqualToString:@"add"]) {

        //NSLog(@"****");

        EditViewController *editVC=segue.destinationViewController;

        editVC.delegate=self;

       // NSLog(@"连线");

    }

    else if([segue.identifierisEqualToString:@"show"])

    {

        

        ShowViewController *showVC=segue.destinationViewController;

        NSIndexPath *indexpath= [self.tableviewindexPathForSelectedRow];

        NSLog(@"indexpath.row=%ld",indexpath.row);

        


        

      if (self.searchDisplay.active)

      {

            showVC.contactPsn=self.searchResult[indexpath.row];

        }

        else

    {

        showVC.contactPsn=self.contacts[indexpath.row];

    }

    }

}



//#pragma mark - searchBar代理方法

//-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

//{

//   // NSLog(@"%@",self.searchbar.text);

//}

//


/*

 下面的方法并没有执行

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar

{

    NSLog(@"searchBarTextDidEndEditing");

    self.IsSearchResult=YES;

    [self.searchDisplay.searchBar resignFirstResponder];

}



-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

{

    NSLog(@"searchBarTextDidBeginEditing");

    self.IsSearchResult=YES;

    [self.searchDisplay.searchBar becomeFirstResponder];

}

//-(void)didPresentSearchController:(UISearchController *)searchController

//{

//    

//}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

{

    NSLog(@"searchBarSearchButtonClicked");

    

    self.IsSearchResult=YES;

}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{

         NSLog(@"searchBarCancelButtonClicked");

    self.IsSearchResult=NO;

}

 */

#pragma mark - UISearchResultsUpdating 代理协议

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    //self.searchDisplay.searchBar

    [self.searchResultremoveAllObjects];

   

    NSPredicate *searchPredicate = [NSPredicatepredicateWithFormat:@"SELF.name CONTAINS[c] %@",self.searchDisplay.searchBar.text];

    self.searchResult = [[self.contactsfilteredArrayUsingPredicate:searchPredicate] mutableCopy];

    dispatch_async(dispatch_get_main_queue(), ^{

        [self.tableviewreloadData];

    });

}

/*UISearchController的移除

viewWillDisappear中要将UISearchController移除,否则切换到下一个View,搜索框仍然会有短暂的存在.*/

//不确定该方法是否有用

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    if (self.searchDisplay.active) {

        self.searchDisplay.active =NO;

        [self.searchDisplay.searchBarremoveFromSuperview];

    }

}


@end


0 0
原创粉丝点击