首页 > 其他分享 >ios开发之--UISearchDisplayController的简单使用

ios开发之--UISearchDisplayController的简单使用

时间:2023-08-22 15:37:43浏览次数:40  
标签:UISearchDisplayController tableView -- self ios cell searchDisplayController sea

控件就不介绍了,UISearchDisplayController就是把searbar和tableview结合到一块了,直接上代码:

.h

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_array;
    NSArray *_filterData;
    UISearchDisplayController *searchDisplayController;
}

@property(nonatomic,strong)UITableView *tableView;

@end

.m

@implementation ThirdViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"搜索框";
    self.view.backgroundColor = [UIColor redColor];
    
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    searchBar.placeholder = @"搜索";
searchBar.delegate = self;
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [[UIView alloc]init];
    self.tableView = tableView;
    [self.view addSubview:self.tableView];
    
    _array = @[@"11",@"A",@"22aa",@"Aa",@"AAa",@"aaA"];
    
    self.tableView.tableHeaderView = searchBar;
    
    //设置搜索栏提示信息
    searchDisplayController.searchBar.prompt = @"搜索提示语";
    //不显示searchbar的边框
    searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    //显示分段条
    searchDisplayController.searchBar.showsScopeBar = YES;
    //分段条的集体内容
    searchDisplayController.searchBar.scopeButtonTitles = @[@"全部",@"高级",@"初级"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.tableView==tableView) {
       return _array.count;
    }else
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchDisplayController.searchBar.text];
        _filterData = [[NSArray alloc]initWithArray:[_array filteredArrayUsingPredicate:predicate]];
        return _filterData.count;
    }
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdenfiter = @"CELLS";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdenfiter];
        cell.textLabel.text = _array[indexPath.row];
    }
    
    if (self.tableView==tableView) {
        cell.textLabel.text = _array[indexPath.row];
    }else
    {
        cell.textLabel.text = _filterData[indexPath.row];
    }
    
    return cell;
}

相关的代理方法:

/搜索代理方法,每次改变搜索内容时都会调用
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"-------%@",searchText);
}

//选择分段时调用
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    NSLog(@"-------%ld",selectedScope);
}

 

 

效果如下图:

ios开发之--UISearchDisplayController的简单使用_搜索

仅做记录!还有其他的几种样式,大家也可以自己尝试下!不过这中原生的效果用的少,自定义的多!



作者:稻草人11223

标签:UISearchDisplayController,tableView,--,self,ios,cell,searchDisplayController,sea
From: https://blog.51cto.com/u_13188203/7190220

相关文章

  • 国标GB28181视频平台EasyGBS国标平台添加针对H.265视频流的告警信息的具体操作流程
    EasyGBS国标视频云服务支持设备/平台通过国标GB28181协议注册接入,可实现视频的实时监控直播、录像、检索与回看、语音对讲、云存储、告警、平台级联等功能。平台支持将接入的视频流进行全终端、全平台分发,分发的视频流包括RTSP、RTMP、FLV、HLS、WebRTC等格式。在EasyGBS平台中接入......
  • swift--动画效果
    一、for循环创建4*4个view,然后对立面的所有view进行动画,这里列集中动画的效果:1,旋转动画fortileinbackgrounds{//现将数字块大小职位原始尺寸的1/10tile.layer.setAffineTransform(CGAffineTransform(scaleX:0.1,y:0.1))......
  • swift--使用 is 和 as 操作符来实现类型检查和转换 / AnyObject与Any的区别
    声明几个类://动物类classAnimal{}//陆地动物类classterricole:Animal{}//海洋动物类classSeaAnimals:Animal{}1,is用来做类型检查letcat=terricole()letfish=SeaAnimals()letarr=[cat,fish]foranima......
  • ios开发之--pop到指定页面
    1推出到根视图控制器[self.navigationControllerpopToRootViewControllerAnimated:YES];2推出到指定的视图控制器 for(UIViewController*controllerinself.navigationController.viewControllers){if([controllerisKindOfClass:[AViewControllerclass]]){......
  • 代码简洁之道:对象转换神器MapStruct
    在我们日常开发的程序中,为了各层之间解耦,一般会定义不同的对象用来在不同层之间传递数据,比如xxxDTO、xxxVO、xxxQO,当在不同层之间传输数据时,不可避免地经常需要将这些对象进行相互转换。今天给大家介绍一个对象转换工具MapStruct,代码简洁安全、性能高,强烈推荐。MapStruct简介MapSt......
  • ios开发之--ios11适配:TableView的heightForHeaderInSection设置高度无效/UISearchBar
    更新到ios11,然后使用x-code9运行项目,发现tableview的-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section方法不走,所以页面也华丽丽的变成了一排的cell,通过查看文档和资料,原来是ios11默认开启self-sizing,把这个属性关系即可,具体代码如下:sel......
  • 用案例带你认识决策树,解锁洞察力
    本文分享自华为云社区《【机器学习|决策树】利用数据的潜力:用决策树解锁洞察力》,作者:计算机魔术师。决策树1.1分类决策树是一种基于树形结构的分类模型,它通过对数据属性的逐步划分,将数据集分成多个小的决策单元。每个小的决策单元都对应着一个叶节点,在该节点上进行分类决......
  • ios开发之--NSString和NSArray互转
    将string字符串转换为array数组NSArray*array=[StrcomponentsSeparatedByString:@","];//分隔符逗号将array数组转换为string字符串 NSString*tempString=[mutableArraycomponentsJoinedByString:@","];//分隔符逗号作者:稻草人11223......
  • swift--触摸(UITouch)事件(点击,移动,抬起)
    触摸事件:UITouch:一个手机第一次点击屏幕,会形成一个UITouch对象,知道离开销毁。表示触碰。UITouch对象能表明当前手指触碰的屏幕位置、状态,状态分为开始触碰、移动、离开。具体方法介绍如下:1.overridefunctouchesBegan(_touches:Set<UITouch>,withevent:UIEvent?)通知调用者当......
  • ios开发之--使用toolbar调整item之间的间隔
    toolbar的item有很多种样式,其实经常使用的就几种,UIBarButtonSystemItemFixedSpace木棍:可以理解为固定的长度UIBarButtonSystemItemFlexibleSpace 弹簧:可以理解为弹性的长度 UIBarButtonItem有两个barbutton的格式  添加到toolbaritems=@[木棍,按钮,弹簧,按钮,木棍];木棍是固定长度......