UITableView + UISearchBar 实现搜索功能

1 #import <UIKit/UIKit.h>
2
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4
5 @property (strong, nonatomic) UIWindow *window;
6
7
8 @end
 1 #import "AppDelegate.h"
 2 #import "RootViewController.h"
 3 @interface AppDelegate ()
 4
 5 @end
 6
 7 @implementation AppDelegate
 8
 9
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
12     // Override point for customization after application launch.
13     self.window.backgroundColor = [UIColor whiteColor];
14
15     self.window.rootViewController = [[RootViewController alloc] init];
16
17     [self.window makeKeyAndVisible];
18     return YES;
19 }
20
21 @end
1 #import <UIKit/UIKit.h>
2
3 @interface RootViewController : UIViewController
4
5 @end
  1 #import "RootViewController.h"
  2 #import "YXPresident.h"
  3 #define Width [UIScreen mainScreen].bounds.size.width
  4 #define Height [UIScreen mainScreen].bounds.size.height
  5 #define gapHeight 20
  6 #define Sheight 50
  7
  8 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate,UISearchBarDelegate>
  9
 10 @property (nonatomic, strong) UISearchBar *searchBar;
 11 @property (nonatomic, strong) UITableView *mTableView;
 12 @property (nonatomic, strong) NSArray *presidents;
 13 @property (nonatomic, strong) NSArray *filteredPresident;
 14
 15 @end
 16
 17 @implementation RootViewController
 18
 19 - (void)loadView
 20 {
 21     [super loadView];
 22     // 初始化searchBar
 23     self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, gapHeight, Width, Sheight)];
 24     self.searchBar.delegate = self;
 25
 26     [self.view addSubview:self.searchBar];
 27     // 初始化mTableView
 28     self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, gapHeight+Sheight, Width, Height - Sheight - gapHeight) style:UITableViewStylePlain];
 29     self.mTableView.dataSource = self;
 30     self.mTableView.delegate = self;
 31     [self.view addSubview:self.mTableView];
 32 }
 33 /**
 34  * 初始化数组
 35  */
 36 - (void)viewDidLoad {
 37     [super viewDidLoad];
 38     self.presidents = [[NSArray alloc] initWithObjects:
 39                        [YXPresident presidentWithFirstName:@"George" lastName:@"Washington"],
 40                        [YXPresident presidentWithFirstName:@"John" lastName:@"Adams"],
 41                        [YXPresident presidentWithFirstName:@"Thomas" lastName:@"Jeffeson"],
 42                        [YXPresident presidentWithFirstName:@"James" lastName:@"Madison"],
 43                        [YXPresident presidentWithFirstName:@"James" lastName:@"Monroe"],
 44                        [YXPresident presidentWithFirstName:@"John Quincy" lastName:@"Adams"],
 45                        [YXPresident presidentWithFirstName:@"Andrew" lastName:@"Jackson"],
 46                        [YXPresident presidentWithFirstName:@"Martin" lastName:@"van Buren"],
 47                        [YXPresident presidentWithFirstName:@"William Henry" lastName:@"Harrison"],
 48                        [YXPresident presidentWithFirstName:@"John" lastName:@"Tyler"],
 49                        [YXPresident presidentWithFirstName:@"James K" lastName:@"Polk"],
 50                        [YXPresident presidentWithFirstName:@"Zachary" lastName:@"Taylor"],
 51                        [YXPresident presidentWithFirstName:@"Millard" lastName:@"Fillmore"],
 52                        [YXPresident presidentWithFirstName:@"Franklin" lastName:@"Pierce"],
 53                        [YXPresident presidentWithFirstName:@"James" lastName:@"Buchanan"],
 54                        [YXPresident presidentWithFirstName:@"Abraham" lastName:@"Lincoln"],
 55                        [YXPresident presidentWithFirstName:@"Andrew" lastName:@"Johnson"],
 56                        [YXPresident presidentWithFirstName:@"Ulysses S" lastName:@"Grant"],
 57                        [YXPresident presidentWithFirstName:@"Rutherford B" lastName:@"Hayes"],
 58                        [YXPresident presidentWithFirstName:@"James A" lastName:@"Garfield"],
 59                        [YXPresident presidentWithFirstName:@"Chester A" lastName:@"Arthur"],
 60                        [YXPresident presidentWithFirstName:@"Grover" lastName:@"Cleveland"],
 61                        [YXPresident presidentWithFirstName:@"Bejamin" lastName:@"Harrison"],
 62                        [YXPresident presidentWithFirstName:@"Grover" lastName:@"Cleveland"],
 63                        [YXPresident presidentWithFirstName:@"William" lastName:@"McKinley"],
 64                        [YXPresident presidentWithFirstName:@"Theodore" lastName:@"Roosevelt"],
 65                        [YXPresident presidentWithFirstName:@"William Howard" lastName:@"Taft"],
 66                        [YXPresident presidentWithFirstName:@"Woodrow" lastName:@"Wilson"],
 67                        [YXPresident presidentWithFirstName:@"Warren G" lastName:@"Harding"],
 68                        [YXPresident presidentWithFirstName:@"Calvin" lastName:@"Coolidge"],
 69                        [YXPresident presidentWithFirstName:@"Herbert" lastName:@"Hoover"],
 70                        [YXPresident presidentWithFirstName:@"Franklin D" lastName:@"Roosevelt"],
 71                        [YXPresident presidentWithFirstName:@"Harry S" lastName:@"Truman"],
 72                        [YXPresident presidentWithFirstName:@"Dwight D" lastName:@"Eisenhower"],
 73                        [YXPresident presidentWithFirstName:@"John F" lastName:@"Kennedy"],
 74                        [YXPresident presidentWithFirstName:@"Lyndon B" lastName:@"Johnson"],
 75                        [YXPresident presidentWithFirstName:@"Richard" lastName:@"Nixon"],
 76                        [YXPresident presidentWithFirstName:@"Gerald" lastName:@"Ford"],
 77                        [YXPresident presidentWithFirstName:@"Jimmy" lastName:@"Carter"],
 78                        [YXPresident presidentWithFirstName:@"Ronald" lastName:@"Reagan"],
 79                        [YXPresident presidentWithFirstName:@"George H W" lastName:@"Bush"],
 80                        [YXPresident presidentWithFirstName:@"Bill" lastName:@"Clinton"],
 81                        [YXPresident presidentWithFirstName:@"George W" lastName:@"Bush"],
 82                        [YXPresident presidentWithFirstName:@"Barack" lastName:@"Obama"],
 83                        nil];
 84
 85 }
 86
 87 #pragma mark - UITableViewDelegate -
 88 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 89 {
 90     if (self.filteredPresident != nil) {
 91         return [self.filteredPresident count];
 92     }
 93     else{
 94         return [self.presidents count];
 95     }
 96 }
 97
 98 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 99 {
100     static NSString *Identify = @"cell";
101     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identify];
102     if (cell == nil) {
103         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identify];
104     }
105     YXPresident *president = [[YXPresident alloc] init];
106     if (self.filteredPresident != nil) {
107         president = [self.filteredPresident objectAtIndex:indexPath.row];
108     }else{
109         president = [self.presidents objectAtIndex:indexPath.row];
110     }
111     if (president) {
112         cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",president.firstName, president.lastName];
113     }
114     return cell;
115 }
116
117 #pragma mark - Content Filtering
118 - (void)filterContentForSearchText:(NSString *)searchText
119 {
120     // 设置搜索条件
121     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName CONTAINS[cd] %@ OR lastName CONTAINS[cd] %@",searchText,searchText];
122     // 返回搜索结果
123     self.filteredPresident = [self.presidents filteredArrayUsingPredicate:predicate];
124 }
125 #pragma mark - UISearchBarDelegate -
126 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
127 {
128     [self filterContentForSearchText:searchText ];
129     if (self.filteredPresident.count == 0 && [searchBar.text  isEqual: @""]) {
130         self.filteredPresident = nil;
131     }
132     [self.mTableView reloadData];
133 }
134
135 @end
 1 #import <Foundation/Foundation.h>
 2
 3 @interface YXPresident : NSObject
 4
 5 @property (nonatomic, copy) NSString *firstName;
 6 @property (nonatomic, copy) NSString *lastName;
 7
 8 + (YXPresident *)presidentWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
 9
10 @end
 1 #import "YXPresident.h"
 2
 3 @implementation YXPresident
 4 @synthesize firstName, lastName;
 5
 6 + (YXPresident *)presidentWithFirstName:(NSString *)firstName lastName:(NSString *)lastName
 7 {
 8     YXPresident *president = [[YXPresident alloc] init];
 9     president.firstName = firstName;
10     president.lastName = lastName;
11     return president;
12 }
13
14 @end
时间: 2024-10-12 14:55:50

UITableView + UISearchBar 实现搜索功能的相关文章

iOS UITableView表格做搜索功能,右边的搜索按钮

当我们阅读一篇文章,肯定过一段时间会忘记,那时候我们就需要用到搜索这个功能,搜索我们当时阅读的文字,用到搜索最多的恐怕是我用到的通讯录, 自从出了微信,一直在想,微信的那个右边顶部的搜索按钮是怎么加的,一直在想,最多想多的要么是一张图片,只能是张图片,如果是图片,那个只能自定义右侧,所以这个方法肯定是可以,还有一种情况,就是自带的方法有这个图片. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {     

ios UISearchDisplayController 实现 UITableView 搜索功能

UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 controller 关联起来,其它的像原 TableView 和搜索结果 TableView 的切换, mask 的显示等等都 封装好了,使用起来非常非常的简单.特别是要实现全屏搜索时使用最多. 全屏搜索的意思是如果你用了  NavigationBar 当点击搜索框时 TableView 会自动弹上去

第四章:IOS Table表视图搜索功能UISearchBar

UISearchBar经常会跟UITable一齐使用,所以在此就介绍一下UISearchBar 先来看看结构 下面再看看它有哪些样式 基本搜索栏.里面????的Search文字用于提示用户??入查询关??字,搜索栏的Placeholder属性可以设置这个提示信息 带有??除按钮的搜索栏.在??入框中??入文字时,会在后面出现??????除按钮,点????除按钮可以??除??入框中的文字 带有查询结果按钮的搜索栏.显示最??搜索结果,显示设定如图4-31所示,选中 Options下的Shows S

iOS.UIKit.14.UITableView -- UISearchBar

1.案例介绍:一个具备搜索功能的表视图,如图01,02,03 图01图02图03 2.Main.storyboard,如图04 图04 3..h #import <UIKit/UIKit.h> @interface CQ23ViewController : UITableViewController<UISearchBarDelegate, UISearchDisplayDelegate> // 搜索栏 @property (weak, nonatomic) IBOutlet UI

iOS8 UISearchViewController搜索功能讲解

在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的使用相比之下简单很多,  需要签订两个代理协议UISearchControllerDelegate, UISearchResultsUpdating.还有一个很重要的属性self.searchVC.active,,返回的BOOL如果为yes,UITableView的数据源应该为搜索后的数组即resu

iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能

iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是不一样的.iOS中的搜索栏实现起来相对简单一点,网上也有很多参考资料,不过靠谱的不是很多,很多都是iOS 8.0之前的实现,iOS 8.0上的实现貌似很少看到,可以运行,不过会看到searchDisplayController' is

【转】为Android应用添加搜索功能

为Android应用添加搜索功能 为Android应用增加搜索功能:增加搜索建议

Yii 1开发日记 ----------- 搜索功能及Checkbox的实现

用yii 1实现后台的搜索功能,效果如下图: 1.模型中: 1 public function search() 2 { 3 4 $criteria = new CDbCriteria; 5 //独立高级搜索 6 if(isset( $_GET['goods']) ) { 7 //商品货号 8 if (isset($_GET['goods']['goods_sn']) && $_GET['goods']['goods_sn'] != "") 9 { 10 $criter

ILSpy搜索功能加强版

1.修改搜索功能,增加如下的额外搜索选项 A.按文本搜索(默认选项) B.按通配符搜索 C.按正则表达式搜索 2.搜索增加如下特性: A.可以按照名字空间检索特定名字空间下的所有类. B.修正了官方版本无法搜索泛型类型的功能. 警告: A.此版本为非官方版本. B.本软件为第三方修改软件,此软件的著作权及版权归原作者所有. C.原软件的任何版权声明及相关权益声明同样适用于本软件. 下载地址: https://onedrive.live.com/?cid=e0560144122a3b9d&id=E