刚学习搜索框控件,写了个最简单的dome

#import <UIKit/UIKit.h>

.h

  1. @interface ViewController : UIViewController<UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>
  2.  
  3. @property (nonatomic,strong) UISearchDisplayController *searchDisplayC;//搜索框控件控制器
  4. @property (weak, nonatomic) IBOutlet UISearchBar *searchBar;//搜索条
  5. @property (nonatomic,strong) NSArray *allArray;//全部数据数组
  6. @property (nonatomic,strong) NSMutableArray *filterArray;//搜索出来的数据数组
  7.  
  8. @end

.m

  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. @synthesize searchBar;
  10. @synthesize searchDisplayC;
  11. @synthesize filterArray;
  12. @synthesize allArray;
  13.  
  14. - (void)viewDidLoad
  15. {
  16. [super viewDidLoad];
  17.  
  18. allArray = [NSArray arrayWithObjects:@"济南",@"天津",@"潍坊",@"上海",@"北京",@"青岛",@"台湾",@"钓鱼岛", nil];
  19. searchDisplayC = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
  20. searchDisplayC.delegate = self;
  21. searchDisplayC.searchResultsDelegate = self;
  22. searchDisplayC.searchResultsDataSource = self;
  23. // Do any additional setup after loading the view, typically from a nib.
  24. }
  25. #pragma mark - tabledelegete
  26.  
  27. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  28. {
  29. return filterArray.count;
  30. }
  31.  
  32. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  33. {
  34. static NSString *identifier = @"Cell";
  35. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  36. if (cell == nil) {
  37. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  38. }
  39. cell.textLabel.text = [filterArray objectAtIndex:indexPath.row];
  40. [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
  41. return cell;
  42. }
  43.  
  44. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  45. {
  46. //选择后要做的事情
  47. NSLog(@"已选择");
  48. }
  49. #pragma mark - searchdelegate
  50.  
  51. - (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  52. {
  53. [filterArray removeAllObjects];
  54. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchString];//用于过滤
  55. filterArray = [NSMutableArray arrayWithArray:[allArray filteredArrayUsingPredicate:predicate]];
  56. return YES;
  57. }
  58.  
  59. - (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
  60. {
  61. //当scope改变时调用
  62. return YES;
  63. }
  64. @end

iOS 搜索框控件 最简单的dome的更多相关文章

  1. IOS中UITextView(多行文本框)控件的简单用法

    1.创建并初始化 UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文.UITextField的用处多,UITextVie ...

  2. IOS自定义日历控件的简单实现(附思想及过程)

    因为程序要求要插入一个日历控件,该空间的要求是从当天开始及以后的六个月内的日历,上网查资料基本上都说只要获取两个条件(当月第一天周几和本月一共有多少天)就可以实现一个简单的日历,剩下的靠自己的简单逻辑 ...

  3. iOS开发--UIKit控件之UISearchBar(搜索栏)

    今天因为需求原因,需要用到搜索控件:之前一直没有用到过这个控件,所以去百度了一下,找到一篇可以说很齐全的资料,感谢这位作者. 然而,我并没有找到可以更改字体大小的属性或方法,希望有知道的告诉我一声,谢 ...

  4. Combo Box (组合框)控件的使用方法

    Combo Box (组合框)控件很简单,可以节省空间.从用户角度来看,这个控件是由一个文本输入控件和一个下拉菜单组成的.用户可以从一个预先定义的列表里选择一个选项,同时也可以直接在文本框里面输入文本 ...

  5. iOS:提示框(警告框)控件UIActionSheet的详解

    提示框(警告框)控件2:UIActionSheet 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.它与导航栏类似,它继承自UIView.   风格类型: ...

  6. iOS:提示框(警告框)控件UIAlertView的详解

    提示框(警告框)控件:UIAlertView   功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.   类型:typedef NS_ENUM(NSInte ...

  7. MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

  8. 从0到1搭建移动App功能自动化测试平台(2):操作iOS应用的控件

    转自:http://debugtalk.com/post/build-app-automated-test-platform-from-0-to-1-Appium-interrogate-iOS-UI ...

  9. VS2010/MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

随机推荐

  1. MFC网页访问的实现示例

    本示例使用MFC 类CInternetSession 建立连接,使用 CHttpFile读取内容. 首先,建立一个MFC对话框项目,界面如下: 1. 添加头文件: #include <afxin ...

  2. CorePlot学习零---安装

    刚開始接触CorePlot时,网上搜到非常多相关文章,解说怎样安装这个第三方库,到眼下阶段该库的版本号已经到了1.5了,可是在github上你能够看到他的安装方法,只是为啥就没有codpod来安装呢? ...

  3. hdu 3874 Necklace(线段树)

    这道题目和我之前做过的一道3xian大牛出的题目很像,不过总的来说还是要简单一点儿. 计算区间内的值的时候如果两个值相等,只能计算其中一个. 这道题需要将所有的问题输入之后再计算,首先,对所有问题的右 ...

  4. Java 通过 BufferReader 实现 文件 写入读取 示例

    package com.javatest.techzero.gui; import java.io.BufferedReader; import java.io.File; import java.i ...

  5. Extjs实现树形结构三连选

    当项目中需要一个部门人员选择或者省市县地域连选时,就需要树形结构的连选. 再此,写了一个简单的树形结构三连选功能,模拟从后台读取数据和处理数据(欢迎大家交流指正). 代码如下: 循环创建三棵树,其中只 ...

  6. myeclispe启动后报错 Subclipse talks to Subversion via a Java API that requires access to native libraries.

    myeclispe 中SVN插件常遇到的异常: Subclipse talks to Subversion via a Java API that requires access to native ...

  7. 在Java中使用MD5和BASE64

    MD5: 在上一篇<Servlet的学习之Session(5)>中,为了能使获取的所有随机数都能有相同的位数,我们采用MD5获取随机数的消息摘要(或称数据指纹.数据摘要等等). MD5可以 ...

  8. HotelIInventory项目小结

    最近参与了HotelIInventory这个项目的一个模块的开发.经验不足,对Sql脚本的编写能力还需要提高,这个模块的Sql语句大多是组长替我写的,如果靠我自己来写,我绝对是没有能力完成工作的,在此 ...

  9. SetWindowLong

    SetWindowLong函数介绍 收藏 SetWindowLong函数介绍 SetWindowLong Unicode 函数原型 LONG SetWindowLong(hwnd,nIndex,lNe ...

  10. 基于visual Studio2013解决面试题之0209最大堆排序

     题目