UISearchBar

#import "MDRootViewController.h"

@interface MDRootViewController ()<UITableViewDelegate,
UITableViewDataSource,UISearchBarDelegate,UISearchDisplayDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableArray *resultArr;
//搜索条
@property(nonatomic,strong)UISearchBar *searchBar;
//显示搜所结果的控制器
@property(nonatomic,strong)UISearchDisplayController *sdc;
@end

@implementation MDRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - 创建数据
-(void)createData
{
    if(!_dataArr)
        _dataArr = [NSMutableArray array];

        NSMutableArray *temp = [NSMutableArray array];
    [temp addObject:@"超哥"];
    [temp addObject:@"击飞"];
    [temp addObject:@"飞哥"];
    [temp addObject:@"亮哥"];
        [self.dataArr addObject:temp];
       //刷新数据
    [self.tableView reloadData];
}

#pragma mark - UI
-(void)createUI
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568-64) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    //创建搜索条
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
   
    self.searchBar.delegate = self;
    self.tableView.tableHeaderView = self.searchBar;
    //实例化搜索结果显示内容
    self.sdc = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    //设置两个委托
    self.sdc.searchResultsDataSource = self;
    self.sdc.searchResultsDelegate = self;
    //把结果存到数据中
    if(!_resultArr)
        _resultArr = [NSMutableArray array];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createUI];
    [self createData];
}

#pragma mark - 黄金三问  dataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //查找模式下就一个分段
    if(tableView != self.tableView)
    {
        return 1;
    }
    else
    {
        //显示原始数据的分段
        return [self.dataArr count];
    }
   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //显示搜索结果的状态
    if(tableView != self.tableView)
    {
        if(_resultArr.count != 0)
           [_resultArr removeAllObjects];
        //开始查找
        for(NSArray *subArr in self.dataArr)
        {
            for(NSString *string in subArr)
            {
                //比较字符串 输入内容
                NSRange range = [string rangeOfString:self.searchBar.text];
                //如果找到要搜索的数据
                if(range.location != NSNotFound)
                {
                    //把数据存到数组中
                    [self.resultArr addObject:string];
                }
            }
        }
        return [self.resultArr count];
    }
    else
    {
        //不是搜索状态显示原始数据
         return [[self.dataArr objectAtIndex:section] count];
    }
  
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //复用的方式就未注册过的方式
    static NSString *cellName = @"Cell";
    //创建一个可以复用的Cell表格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    //有可能会有空cell
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    //状态判断
    if(tableView != self.tableView)
    {
        //是搜索状态
        cell.textLabel.text = [self.resultArr objectAtIndex:indexPath.row];
    }
    else
    {
        //没有处于搜索状态就显示数据源的数据
        cell.textLabel.text = [[self.dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }
    //返回cell实例
    return  cell;
}

//点击按钮被取消
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"取消按钮被点击");
}

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"AAAA");

    return YES;
}

转载于:https://www.cnblogs.com/4Dream/p/4563198.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值