#import "RootController.h"@interface RootController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>@property (nonatomic, retain) NSMutableArray *dataList; // 存储数据源
@property (nonatomic, retain) NSMutableArray *searchList; // 存储搜索结果
@property (nonatomic, retain) UISearchController *searchController; // 管理搜索框

@end@implementation RootController- (void)dealloc {self.dataList = nil;self.searchList = nil;self.searchController = nil;[super dealloc];
}// 懒加载
- (NSMutableArray *)dataList {if (!_dataList) {self.dataList = [NSMutableArray arrayWithCapacity:1];for (int i = 0; i < 50; i++) {[self.dataList addObject:[NSString stringWithFormat:@"%d - lanou", i]];}}return [[_dataList retain] autorelease];
}- (NSMutableArray *)searchList {if (!_searchList) {self.searchList = [NSMutableArray arrayWithCapacity:1];}return [[_searchList retain] autorelease];
}- (void)viewDidLoad {[super viewDidLoad];self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];// 设置负责更新搜索结果控制器内容的对象, 要遵守协议<UISearchResultsUpdating>self.searchController.searchResultsUpdater = self;// 在搜索的时候下面的内容是否变暗self.searchController.dimsBackgroundDuringPresentation = NO;// 在搜索的时候导航栏是否隐藏self.searchController.hidesNavigationBarDuringPresentation = NO;// 设置frameself.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);// 将tableView 的页眉设置为searchBarself.tableView.tableHeaderView = self.searchController.searchBar;//    self.searchController.searchBar.showsScopeBar = YES;
//    NSArray *titles = @[@"1", @"2"];
//    self.searchController.searchBar.scopeButtonTitles = titles;// 设置背景图片self.searchController.searchBar.backgroundImage = [UIImage imageNamed:@"banner"];// 设置背景颜色// 移除searchBarBackground
//    [[self.searchController.searchBar.subviews objectAtIndex:0] removeFromSuperview];
//    self.searchController.searchBar.backgroundColor = [UIColor redColor];// 样式
//    self.searchController.searchBar.barStyle = UIBarStyleDefault;
//    self.searchController.searchBar.barStyle = UIBarStyleBlackOpaque;
    [self.searchController release];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (BOOL)prefersStatusBarHidden {return YES;
}#pragma mark -- UISearchBarDelegate
// 编辑输入事件
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {return YES;
}// return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {}// called when text starts editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {return YES;
}// return NO to not resign first responder
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {}// called when text ends editing
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {}// called when text changes (including clear)
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0) {return YES;
}// called before text changes// 点击按钮事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {}// called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {}// called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {}// called when cancel button pressed
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2) {}// called when search results button pressed// Scope 点击事件
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0) {}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.// Return the number of sections.return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.// Return the number of rows in the section.// 如果对搜索框有操作if(self.searchController.active) {return [self.searchList count];}else {// 如果对搜索框没有操作return [self.dataList count];}
}#pragma mark -- UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {// 谓词 NSPredicate -- 它的操作是针对于数组类型的, 就好比数据库中的查询, 数据源就是数组, 好处是: 不需要写很多代码去操作数组, 同时也起到了过滤的作用. 我们可以编写简单的谓词语句, 就可以从数组中过滤出想要的数据.NSString *searchStr = [self.searchController.searchBar text];NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchStr];// SELF CONTAINS[c] %@, SELF表示要查询集合对象, CONTAINS[c]表示包含字符串, 其中c 不区分大小写.if (self.searchList != nil) {[self.searchList removeAllObjects];}// 过滤数据self.searchList = [NSMutableArray arrayWithArray:[self.dataList filteredArrayUsingPredicate:predicate]];// 刷新表格
    [self.tableView reloadData];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *identifer = @"reuse";// 根据重用标志, 去tableView的重用队列中取可用的cellUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifer];}// cell显示的内容if (self.searchController.active) {cell.textLabel.text = self.searchList[indexPath.row];}else {cell.textLabel.text = self.dataList[indexPath.row];}// Configure the cell...return cell;
}@end

转载于:https://www.cnblogs.com/wohaoxue/p/4819917.html

iOS中的UISearchBar相关推荐

  1. iOS中利用UISearchBar实现搜索

    先把源码贴出来 https://github.com/losedMemory/ZSSearchBar   这是我在github上写的一个Demo,大家可以看看 在大多数app中都会用到搜索功能,那么搜 ...

  2. iOS中UISearchBar(搜索框)使用总结

    2019独角兽企业重金招聘Python工程师标准>>> iOS中UISearchBar(搜索框)使用总结 初始化:UISearchBar继承于UIView,我们可以像创建View那样 ...

  3. iOS中持久化存储SQLite(一)

    在iOS中做持久化存储有多种方案,其中包括plist, preference, sqlite, core data,其中: (1)plist, preference适合小型数据存储,因为每次存储前都必 ...

  4. iOS中 UISearchController 搜索栏 UI技术分享

    iOS中 UISearchController 搜索栏 UI技术分享 [objc] view plain copy <p style="margin-top: 0px; margin- ...

  5. iphone smtp服务器没有响应,电子邮件卡在iPhone或iPad上的发件箱?如何修复iOS中的未发送邮件 | MOS86...

    您曾经在iOS中发送电子邮件,只能将信息卡在iPhone,iPad或iPod touch的邮件应用发件箱中?你知道这是什么时候发生的,因为在iOS的Mail应用程序的底部,状态栏在iOS中显示1个未发 ...

  6. mui ios中form表单中点击输入框头部导航栏被推起及ios中form表单中同时存在日期选择及输入框时,日历选择页面错乱bug...

    一.ios header导航栏被推起解决方法 1 设置弹出软键盘时自动改变webview的高度 plus.webview.currentWebview().setStyle({ softinputMo ...

  7. iOS中几种数据持久化方案总结

    概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...

  8. iOS中关于NSTimer使用知多少

    看到这个标题,你可能会想NSTimer不就是计时器吗,谁不会用,不就是一个能够定时的完成任务的东西吗? 我想说你知道NSTimer会retain你添加调用方法的对象吗?你知道NSTimer是要加到ru ...

  9. iOS中JS 与OC的交互(JavaScriptCore.framework)

    iOS中实现js与oc的交互,目前网上也有不少流行的开源解决方案: 如:react native 当然一些轻量级的任务使用系统提供的UIWebView 以及JavaScriptCore.framewo ...

  10. 在iOS中使用tableView

    为什么80%的码农都做不了架构师?>>>    UITableView是iOS中最常用的控件了,所以使用起来也很简单. ViewContoller.h 文件 (继承UITableVi ...

最新文章

  1. java三角形创建子类_如何创建子类,以便参数属于Java中的子类类型
  2. python3 json模块操作
  3. mybatisplus的详细使用(自动填充,乐观锁,分页,条件查询)
  4. 排序算法 —— 冒泡排序
  5. Cannot find source code based button in SE24 - modification assistant
  6. Php通过gsoap调用c++ websevice
  7. childNodes在IE与Firefox中的区别
  8. Axure高保真web端后台管理系统/垃圾回收分类系统/垃圾回收高保真原型设计 /垃圾分类后台管理系统/垃圾回收分类平台//垃圾回收分类智慧管理系统/订单管理/财务管理/系统管理/库存管理/设备管理
  9. Scheme学习系列O:启动篇
  10. jdk HashMap源码解读
  11. zjufantasy.com开发日记(1)
  12. 8个免费在线PDF转Word工具,一键轻松转换
  13. 算法面试基础:LR(逻辑回归)
  14. PSCC2019常用基础操作
  15. 搜狗收录方法之搜狗推送接口实现
  16. 20几岁要懂点经济学【笔记】
  17. 基于MODIS数据的滁州市冬小麦长势遥感监测研究
  18. 俄罗斯天才少女也选华为,22岁拿下世界编程冠军,同天队友也宣布加盟
  19. linux下electron任务栏图标处理
  20. 图纸中bs是什么意思_园建施工图中WL、BL、FL、TW、SL分别是什么意思

热门文章

  1. 信息安全等级保护工作概述
  2. MySQL复制 自动监控脚本
  3. 字节实习生开发的 AI 竟然被网友用在了王冰冰身上!
  4. Nginx 的 5 大应用场景,太实用了!
  5. 架构师必备最全SQL优化方案
  6. linux unix域socket_计算机通信之谜,带你彻底理解socket网络编程(一)
  7. Linux进阶之补充知识篇
  8. 「HDU6583 Typewriter」 - 后缀自动机
  9. 第二十八篇 闭包函数
  10. postman的基础使用