如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS

那我们开始吧,下面是Sely写的一个Demo,分享给大家。

新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死板了,达不到想要的效果。

这里进行重新定制, 四个协议, 三个成员变量,第一步OK。

@interface ViewController ()<UISearchBarDelegate,UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>

{

UISearchBar *mySearchBar;

UISearchDisplayController *mySearchDisplayController;

NSMutableArray *suggestResults;

}

@end

//下面我们来初始化我们的三个成员

-(void)initMysearchBarAndMysearchDisPlay

{

mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, [UIApplicationsharedApplication].statusBarFrame.size.height, [UIScreen mainScreen].bounds.size.width, 44)];

mySearchBar.delegate = self;

//设置选项

mySearchBar.barTintColor = [UIColor redColor];

mySearchBar.searchBarStyle = UISearchBarStyleDefault;

mySearchBar.translucent = NO; //是否半透明

[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];

[mySearchBar sizeToFit];

mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];

mySearchDisplayController.delegate = self;

mySearchDisplayController.searchResultsDataSource = self;

mySearchDisplayController.searchResultsDelegate = self;

suggestResults = [NSMutableArray arrayWithArray:@[@"此处为推荐词", @"也可以为历史记录"]];

}

//然后呢当然是自定义我们的navigation bar了

-(void)initNavigationBar

{

UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

[moreButton setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];

[moreButton addTarget:self action:@selector(handleMore:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *moreItem = [[UIBarButtonItem alloc] initWithCustomView:moreButton];

UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

[searchButton setImage:[UIImage imageNamed:@"info_search"] forState:UIControlStateNormal];

[searchButton addTarget:self action:@selector(startSearch:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];

self.navigationItem.rightBarButtonItems = @[moreItem, searchItem];

}

//好了, 开始加载界面了

- (void)viewDidLoad

{

[super viewDidLoad];

[self initNavigationBar];

[self initMysearchBarAndMysearchDisPlay];

}

//实现我们的点击事件

#pragma mark - handle button click

-(void) startSearch:(id)sender

{

[self.navigationController.view addSubview:mySearchBar];

[mySearchBar becomeFirstResponder];

}

-(void) handleMore:(id)sender

{}

//实现我们的table view 的协议,判断当我们开始搜索时,转到自己实现的一个searchDisplayController方法里,这样代码看起来是不是很简洁?

#pragma mark - UITableViewDataSource & UITableViewDelegate

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if (tableView == mySearchDisplayController.searchResultsTableView)

{

return [self numberOfRowsWithSearchResultTableView];

}

return 0;

}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (tableView == mySearchDisplayController.searchResultsTableView)

{

return [self searchTableView:mySearchDisplayController.searchResultsTableView cellForRowAtIndexPath:indexPath];

}

else

{

static NSString *cellID = @"search_cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell == nil)

{

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

}

return cell;

}

}

//这里专门写UISearchDisplayController 中 searchResultsTableView 的 data source 和 delegate

#pragma mark - UISearchDisplayController   <UITableViewDataSource> dataSource

-(NSInteger)numberOfRowsWithSearchResultTableView

{

return suggestResults.count + 1;

}

-(UITableViewCell*)searchTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *suggestId = @"suggestCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:suggestId];

if (cell == nil)

{

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:suggestId];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}

if (indexPath.row == suggestResults.count)

{

cell.textLabel.text = [NSLocalizedString(@"Search: ", @"查找: ") stringByAppendingString:mySearchBar.text];

}

else

{

cell.textLabel.text = [suggestResults objectAtIndex:indexPath.row];

}

return cell;

}

#pragma mark - UISearchDisplayController <UITableViewDelegate> delegate

-(void) searchTableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *keyword = nil;

if (indexPath.row == suggestResults.count)

{

keyword = mySearchBar.text;

}

else

{

keyword = [suggestResults objectAtIndex:indexPath.row];

}

[mySearchBar resignFirstResponder];

}

//开始我们的search bar delegate,输入搜索内容, 因为是Demo ,所以我并没有搜索结果,这个大家根据需求自己实现吧

#pragma mark - UISearchBarDelegate

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar

{

return YES;

}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

{

}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

[mySearchDisplayController.searchResultsTableView reloadData];

}

//最后的环节了, 这个地方的逻辑就会千变万化了

#pragma mark - UISearchDisplayDelegate

- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0)

{

//开始搜索事件,设置searchResultsTableView的contentInset,否则位置会错误

mySearchDisplayController.searchResultsTableView.contentInset = UIEdgeInsetsMake(mySearchBar.frame.size.height, 0, 0, 0);

}

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableViewNS_DEPRECATED_IOS(3_0,8_0)

{

//加载searchResultsTableView完毕

}

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0)

{

//结束搜索事件,移除mySearchBar

[mySearchBar removeFromSuperview];

}

// return YES to reload table. called when search string/option changes. convenience methods on top UISearchBar delegate methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString*)searchString NS_DEPRECATED_IOS(3_0,8_0)

{

//是否需要刷新searchResultsTableView

if(@"符合条件")

{

return YES;

}

return NO;

}

就到这里结束,是不是很容易? 这是第一次发布,如果做的不好,请大神指正,如果文笔不好(估计是肯定的),也请多指教,最后希望能多多帮助到大家。

Sely 原创

时间: 2024-08-09 10:52:27

如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS的相关文章

在UINavigation上添加UISearchBar

在UINavigation上添加UISearchBar的方法 代码如下: 1 // 创建UIView 2 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 250, 54)]; 3 [self.view addSubview:view]; 4 // 创建搜索栏 5 UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 10, 250

如何在WIN7上添加磁盘

1.右键计算机--管理--存储--磁盘管理. 2.右击你要分割的磁盘C--选择"压缩卷",打开压缩C盘空间对话框--在"输入压缩空间量"中输入分区要减少的容量--单击"压缩"按钮开始压缩. 3.压缩完成后,在原分区后面会出现"可用空间"--右击"可用空间"--选择"新建简单卷"--单击"下一步"--输入新分区的容量--单击"下一步"--分配分区的盘

图解如何在DC上添加自定义属性类

刚才在群里,看到大家问了个AD 自定义属性的问题,现在我来给大家解答下,大家可能都遇到过这样的场景: 1. 在工作上,需要在人员上增加一些属性,比如:性别.是否婚配.年龄.性取向-.(请忽视) 2. 我们不只想要自定义一些人员属性,还想给组.计算机.其他对象也需要定义一些属性,比如说,这个组是否为领导组.排序排多少位等等 我先来说个真实的例子,我去年在做项目上遇到个需求,XX公司要求在SharePoint 上按照公司.部门优先级做为顺序显示组织结构.部门点开后,里面的人员还要求领导要排在上面,然

如何在IDEA上 添加GIT和maven、mybatis插件

IDEA工具上,添加GIT和maven.mybatis插件,相对比较简单: 首先下载GIT.maven.mybatis. 先添加GIT插件: 首先在IDEA找到file中找到setting,然后搜索git,接着将git存放的路径找到即可. 接着是maven插件,也是很简单,打开setting设置,搜索maven,接着路径改了就可以 mybatis的配置,点击run,找到Edit Configurations 接着只需要配置一下即可,在Command line这一行输入mybatis-genera

如何在UITableViewController上添加一个固定的视图

最近在使用UITableViewController,想在上面添加一个固定的视图,不随ScrollView滑动而移动.最后找到2种解决办法,一种是计算TableView的偏移,调整视图的位置,不断更新视图,从而达到相对静止.使用UIScrollViewDelegate里的方法-(void) scrollViewDidScroll; -(void)scrollViewDidScroll:(UIScrollView *)scrollView { self.hud.frame = CGRectMake

以horovd的HorovodAllreduceOp为例,学习如何在tensorflow上添加一个新的操作OP

参考: http://www.tensorfly.cn/tfdoc/how_tos/adding_an_op.html 添加新的OP需要3步(下述所有代码在here): 1. 定义 Op 的接口 // 1. 定义 Op 的接口 // REGISTER_OP()向 TensorFlow 系统注册来定义 Op 的接口,该OP就是HorovodAllreduceOp. // 在注册时, 指定 Op 的名称: REGISTER_OP("HorovodAllreduce") // 输入(类型和名

如何在vmvare上添加共享磁盘

我们在个人计算机上搭建rac时,经常会因为共享磁盘而卡壳儿,下面是如何直接添加本地共享磁盘的方法: ********************* 在cmd下切换到vmvare安装目录下 ********************* vmware-vdiskmanager.exe -c -s 2g -a lsilogic -t 2 "H:\share disk\ocr.vmdk" vmware-vdiskmanager.exe -c -s 2g -a lsilogic -t 2 "

android如何在textview或edittext上添加表情

先上效果图: 首先来写一个表情的GridView public class EmotionView extends LinearLayout implements OnItemClickListener { private GridView mGridView; private static final ArrayList<Integer> emotionDisplayList = new ArrayList<Integer>(); public static final Link

如何在xshell会话上添加新端口转发规则

为使用通过加密隧道的端口转发服务,应定义各个应用程序的端口转发规则.本集主要讲解如何在xshell会话中添加新端口转发规则. 如何在会话上添加新端口转发规则: 1.打开会话对话框. 2.选择你要编辑端口转发规则的会话. 3.点击标准按钮的[属性]. 4.在[类别]中选择[连接: SSH: 隧道]. 5.点击[添加]打开转发规则对话框. 6.在[类型]目录选择连接类型. 7. 在[源主机]中输入localhost或IP地址. 8.在[侦听端口]中输入端口号或选择服务名称. 9.在[目标主机]中输入