最近在项目开发中用到了搜索框,之前都是用的系统的searchbar,现有项目中用的是EVNCustomSearchBar,我试了一下还挺方便,下面说一下具体的用法。

  第一步:引入添加相关的委托代理EVNCustomSearchBarDelegate,懒加载初始化对象

  

if (!_searchBar) {

_searchBar = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(16, 0, K_CC_SCREEN_WIDTH-32, 44)];

_searchBar.backgroundColor = K_CC_COLOR_STRING(@"#F5F5F5");

_searchBar.textColor = K_CC_COLOR_STRING(@"#999999");

_searchBar.textFieldColor = [UIColor clearColor];

_searchBar.iconImage = K_CC_IMAGE(@"newhome_search");

_searchBar.iconAlign = EVNCustomSearchBarIconAlignCenter;

[_searchBar setPlaceholder:K_CC_LOCAL_STR(@"home.search")];// 搜索框的占位符

_searchBar.placeholderColor = K_CC_COLOR_STRING(@"#999999");

_searchBar.delegate = self; //设置代理

_searchBar.isHiddenCancelButton = YES;

_searchBar.tintColor = K_CC_COLOR_STRING(@"#999999");

[_searchBar sizeToFit];

}

return _searchBar;

  第二步:添加到相应的view上

  

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_CC_SCREEN_WIDTH, 44)];

headerView.backgroundColor = [UIColor clearColor];

[headerView addSubview:self.searchBar];

self.distributeTable.tableHeaderView = headerView;

  第三步:添加相关的搜索代理逻辑处理

  

#pragma mark - EVNCustomSearchBarDelegate -

- (void)searchBar:(EVNCustomSearchBar *)searchBar textDidChange:(NSString *)searchText {

if (searchText.length > 0) {

// 根据模型属性搜索结果

self.searchList=[NSMutableArray arrayWithArray:[[NDSearchTool tool] searchWithFieldArray:@[@"username"] inputString:searchText inArray:self.dataAddressList]];

self.isSearch = YES;

}else{

self.isSearch = NO;

self.currentSeasSearchItem=-1;

//清空搜索内容Id

[self.searchList removeAllObjects];

}

[self.distributeTable reloadData];

}

- (NSArray *)searchWithFieldArray:(NSArray *)fieldArray

inputString:(NSString *)inputString

inArray:(NSArray *)array

{

if (![array count] || ![fieldArray count])

{

return nil;

}

NSPredicate *scopePredicate;

NSMutableArray *backArray = [NSMutableArray array];

for (NSString *fieldString in fieldArray)

{

NSArray *tempArray;

scopePredicate = [NSPredicate predicateWithFormat:@"SELF.%@ contains[c] %@", fieldString, inputString];

tempArray = [array filteredArrayUsingPredicate:scopePredicate];

for (NSObject *object in tempArray)

{

if (![backArray containsObject:object])

{

[backArray nd_addObj:object];

}

}

}

return backArray;

}

  完成这些就可以实现动态的模糊搜索,大功告成。

iOS中搜索框EVNCustomSearchBar使用小结的更多相关文章

  1. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  2. ios UISearchBar搜索框的基本使用

    摘要: 小巧简洁的原生搜索框,漂亮而易用,如果我们的应用没有特殊需求,都可以使用它. iOS中UISearchBar(搜索框)使用总结 初始化:UISearchBar继承于UIView,我们可以像创建 ...

  3. ArcGIS API for javascript中搜索框的使用问题

    我们在开发中常常需要用搜索框去搜索地图上的某个数据,然后在地图上显示出来.这个时候我们要用到esri.dijit.Search().在设置里面的sources的时候,需要注意一点:必须要使用在线的fe ...

  4. Android 自定义EditText实现类iOS风格搜索框

    最近在项目中有使用到搜索框的地方,由于其样式要求与iOS的UISearchBar的风格一致.默认情况下,搜索图标和文字是居中的,在获取焦点的时候,图标和文字左移.但是在Android是并没有这样的控件 ...

  5. IOS中文本框输入自动隐藏和自动显示

    uilabe和UIText扩展方法 +(UILabel*)LabWithFrame:(CGRect)_rect text:(NSString*)aText textColor:(UIColor*)aC ...

  6. 解决select2 在modal中搜索框无效的问题

    $.fn.modal.Constructor.prototype.enforceFocus = function() {};

  7. iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  8. UISearchBar改变搜索框的高度

    系统的searchBar UISearchBar的中子控件及其布局 UIView(直接子控件) frame 等于 searchBar的bounds,view的子控件及其布局 UISearchBarBa ...

  9. vue中搜索关键词,使文本标红

    UserHead.vue中搜索框: <!-- 搜索 --> <el-col :span="6" :offset="8" class=" ...

  10. 带输入提示的搜索框ajax请求

    先放图 首先要引用的文件有: base.css  https://www.cnblogs.com/chenyingying0/p/12363689.html jquery.js transition. ...

随机推荐

  1. win10安装和使用wireshark

    win10安装和使用wiresharkhttps://blog.csdn.net/qq_34732729/article/details/105126146https://blog.csdn.net/ ...

  2. app专项测试:app弱网测试(测试工具)

    app专项测试:app弱网测试(测试工具) 除了常用的 fiddler,charles 可以模拟弱网,还有硬件工具弱网仪 HoloWAN也可以模拟弱网 使用弱网仪有以下优点:1.即插即用,无需调试和复 ...

  3. 【CentOS】 8版本 Cannot update read-only repo 问题

    GUI界面应用市场无法访问 https://blog.csdn.net/hm0406120201/article/details/104553205/

  4. 【Vue】02 Component 组件 & Axios

    Vue自定义组件: 不论任何注册组件的方式:template属性有且仅有一个根节点标签 就是说模版属性的标签只能有一个在最外面 <div id="container-element&q ...

  5. 如何在 Ubuntu18.04 server 服务器版本的操作系统下 配置IP

    如题,现有需求,为一个server版本的Ubuntu18.04配置 IP . 在网上查到了     Ubuntu18.04  桌面版本  的配置方法: https://www.cnblogs.com/ ...

  6. 【转载】 Python格式化字符串f-string概览

    版权声明:本文为CSDN博主「sunxb10」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/sunxb10/a ...

  7. 【转载】 解决VSCode运行或调试python文件无反应的问题

    ---------------- 版权声明:本文为CSDN博主「姜行者」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn. ...

  8. mybatis-plus系统化学习之配置精讲

    1.背景 mybatis-plus给出了很多配置, 大部分的配置使用默认的就可以了, 但是还是有很多需要的配置比如: # mybatis-plus相关配置 mybatis-plus: # xml扫描, ...

  9. JAVA for Cplex(更新版)

    一.Cplex的介绍 Cplex是一种专门用来求解大规模线性规划问题的求解工具.不仅仅是LP问题,对于二次规划 QP,二次有约束规划QCP,混合整数线性规划MIP问题,甚至Network Flow问题 ...

  10. Flutter&Dart Callback转同步

    前言 怎么将一个Callback回调转化成Future同步方法(Callback to Future),可以配套async / await去使用呢? 个人觉得,这是一个很常见的现象,不知道为啥,很多人 ...