UISearchController搜索功能的简单使用

本文将介绍如何在iOS应用中使用UISearchController实现搜索功能,包括创建UISearchController、配置数据源和筛选数据的过程。适用于iOS8及以上版本。

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

UISearchController是iOS8出的

如果要适配iphone4的朋友,就别使用UISearchController,4不能升级到iOS8。

直接上代码

#import "ViewController.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>


@property (nonatomic,strong) UISearchController *searchController;


@property (nonatomic,strong)UITableView *tableView;


//数据源

@property (strong,nonatomic)NSMutableArray  *dataList;


//筛选后的数据

@property (strong,nonatomic)NSMutableArray  *searchList;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

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

    

    // 创建tableView

    self.tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height)style:UITableViewStylePlain];

    self.tableView.dataSource =self;

    self.tableView.delegate =self;

    [self.viewaddSubview:self.tableView];

    

    // 创建UISearchController

    _searchController = [[UISearchControlleralloc] initWithSearchResultsController:nil];

    _searchController.searchResultsUpdater =self;

    _searchController.dimsBackgroundDuringPresentation =NO;

    _searchController.hidesNavigationBarDuringPresentation =NO;

    _searchController.searchBar.frame =CGRectMake(self.searchController.searchBar.frame.origin.x,self.searchController.searchBar.frame.origin.y,self.searchController.searchBar.frame.size.width, 44.0);

    self.tableView.tableHeaderView =self.searchController.searchBar;

    

    

    self.dataList=[NSMutableArrayarrayWithCapacity:100];

    

    for (NSInteger i=0; i<100; i++) {

        [self.dataListaddObject:[NSStringstringWithFormat:@"%ld-俊彩飞扬",(long)i]];

    }

    

    

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}


// self.searchController.active 当前的状态

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

    if (self.searchController.active) {

        return [self.searchListcount];

    }else{

        return [self.dataListcount];

    }

}


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

    static NSString *flag=@"cellFlag";

    UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:flag];

    if (cell==nil) {

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

    }

    if (self.searchController.active) {

        [cell.textLabel setText:self.searchList[indexPath.row]];

    }

    else{

        [cell.textLabel setText:self.dataList[indexPath.row]];

    }

    return cell;

}


- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{

    NSLog(@"搜索Begin");

    return YES;

}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{

    NSLog(@"搜索End");

    return YES;

}


-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {

    NSString *searchString = [self.searchController.searchBartext];

    // 筛选谓词

    NSPredicate *preicate = [NSPredicatepredicateWithFormat:@"SELF CONTAINS[c] %@", searchString];

    if (self.searchList!=nil) {

        [self.searchListremoveAllObjects];

    }

    //过滤数据

    self.searchList= [NSMutableArrayarrayWithArray:[_dataListfilteredArrayUsingPredicate:preicate]];

    //刷新表格

    [self.tableViewreloadData];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值