iOS UISearchController 搜尋方塊

來源:互聯網
上載者:User

標籤:

#import <Foundation/Foundation.h>

 

@interface Student : NSObject

 

@property(strong,nonatomic) NSString *name;

@property(strong,nonatomic) NSString *pic;

@property(strong,nonatomic) NSString *tel;

 

-(Student *)initWithDic:(NSDictionary *)dic;

+(Student *)studentWithDic:(NSDictionary *)dic;

@end

 

#import "Student.h"

 

@implementation Student

 

-(Student *)initWithDic:(NSDictionary *)dic

{

 self= [super init];

    if (self)

    {

        self.name=dic[@"name"];

        self.pic=dic[@"pic"];

        self.tel=dic[@"tel"];

    }

    

    return self;

}

 

+(Student *)studentWithDic:(NSDictionary *)dic

{

    return [[self alloc] initWithDic:dic];

}

 

-(NSString *)description

{

    return [NSString stringWithFormat:@"name=%@,pic=%@,tel=%@",_name,_pic,_tel];

}

 

@end

 

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>

 

@property(strong,nonatomic) NSMutableArray *arrData;

 

@property(strong,nonatomic) UITableView *tableV;

 

@property(strong,nonatomic) UISearchController *searchController;

 

@property(strong,nonatomic) NSArray *arrTemp;

 

 

@end

 

#import "ViewController.h"

#import "Student.h"

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.arrData=[NSMutableArray array];

    

    

    for (NSArray *arr in [[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"GYiOSclass(2)" ofType:@"plist"]] allValues])

    {

        for (NSDictionary *dic in arr)

        {

            [self.arrData addObject:[Student studentWithDic:dic]];

        }

    }

    

    self.tableV=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

    self.tableV.delegate=self;

    self.tableV.dataSource=self;

    [self.view addSubview:self.tableV];

   

    self.searchController=[[UISearchController alloc] initWithSearchResultsController:nil];

    //指定代理

    self.searchController.searchResultsUpdater=self;

    //搜尋方塊的背景色

    self.searchController.searchBar.backgroundColor=[UIColor lightGrayColor];

    //將

    self.tableV.tableHeaderView=self.searchController.searchBar;

    

}

 

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

{

    if (self.searchController.active)

    {

        return self.arrTemp.count;

    }

    else

    {

        return self.arrData.count;

    }

}

 

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

{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    if (cell==nil)

    {

        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyCell"];

    }

    if (self.searchController.active)

    {

        cell.textLabel.text=[self.arrTemp[indexPath.row] name];

        cell.detailTextLabel.text = nil;

        cell.imageView.image=nil;

 

    }

    else

    {

        cell.textLabel.text=[self.arrData[indexPath.row] name];

        cell.imageView.image=[UIImage imageNamed:[self.arrData[indexPath.row] pic]];

        cell.detailTextLabel.text=[self.arrData[indexPath.row] tel];

    }

    return cell;

}

 

//實現UISearchResultsUpdating 代理

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    //謂詞

    

    NSPredicate *pre=[NSPredicate predicateWithFormat:@"name=%@",self.searchController.searchBar.text];

    NSLog(@"%@",pre);

    self.arrTemp=[self.arrData filteredArrayUsingPredicate:pre];

    

    dispatch_async(dispatch_get_main_queue(),^

                   {

                       [self.tableV reloadData];

                   });

    

    NSLog(@"%@",self.arrTemp);

}

 

//

-(void)viewDidDisappear:(BOOL)animated

{

    [super viewDidDisappear:animated];

    if (self.searchController.active)

    {

        self.searchController.active=NO;

        [self.searchController.searchBar removeFromSuperview];

    }

}

 

@end

 

iOS UISearchController 搜尋方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.