如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 OC iOSWord下载.docx
- 文档编号:16930627
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:7
- 大小:16.64KB
如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 OC iOSWord下载.docx
《如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 OC iOSWord下载.docx》由会员分享,可在线阅读,更多相关《如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 OC iOSWord下载.docx(7页珍藏版)》请在冰豆网上搜索。
-(void)initMysearchBarAndMysearchDisPlay
mySearchBar=[[UISearchBaralloc]initWithFrame:
CGRectMake(0,[UIApplicationsharedApplication].statusBarFrame.size.height,[UIScreenmainScreen].bounds.size.width,44)];
mySearchBar.delegate=self;
//设置选项
mySearchBar.barTintColor=[UIColorredColor];
mySearchBar.searchBarStyle=UISearchBarStyleDefault;
mySearchBar.translucent=NO;
//是否半透明
[mySearchBarsetAutocapitalizationType:
UITextAutocapitalizationTypeNone];
[mySearchBarsizeToFit];
mySearchDisplayController=[[UISearchDisplayControlleralloc]initWithSearchBar:
mySearchBarcontentsController:
self];
mySearchDisplayController.delegate=self;
mySearchDisplayController.searchResultsDataSource=self;
mySearchDisplayController.searchResultsDelegate=self;
suggestResults=[NSMutableArrayarrayWithArray:
@[@"
此处为推荐词"
@"
也可以为历史记录"
]];
//然后呢当然是自定义我们的navigationbar了
-(void)initNavigationBar
UIButton*moreButton=[[UIButtonalloc]initWithFrame:
CGRectMake(0,0,30,30)];
[moreButtonsetImage:
[UIImageimageNamed:
@"
more"
]forState:
UIControlStateNormal];
[moreButtonaddTarget:
selfaction:
@selector(handleMore:
)forControlEvents:
UIControlEventTouchUpInside];
UIBarButtonItem*moreItem=[[UIBarButtonItemalloc]initWithCustomView:
moreButton];
UIButton*searchButton=[[UIButtonalloc]initWithFrame:
[searchButtonsetImage:
info_search"
[searchButtonaddTarget:
@selector(startSearch:
UIBarButtonItem*searchItem=[[UIBarButtonItemalloc]initWithCustomView:
searchButton];
self.navigationItem.rightBarButtonItems=@[moreItem,searchItem];
//好了,开始加载界面了
-(void)viewDidLoad
[superviewDidLoad];
[selfinitNavigationBar];
[selfinitMysearchBarAndMysearchDisPlay];
}
//实现我们的点击事件
#pragmamark-handlebuttonclick
-(void)startSearch:
(id)sender
[self.navigationController.viewaddSubview:
mySearchBar];
[mySearchBarbecomeFirstResponder];
-(void)handleMore:
{}
//实现我们的tableview的协议,判断当我们开始搜索时,转到自己实现的一个searchDisplayController方法里,这样代码看起来是不是很简洁?
#pragmamark-UITableViewDataSource&
amp;
UITableViewDelegate
-(NSInteger)tableView:
(UITableView*)tableViewnumberOfRowsInSection:
(NSInteger)section
if(tableView==mySearchDisplayController.searchResultsTableView)
{
return[selfnumberOfRowsWithSearchResultTableView];
}
return0;
-(UITableViewCell*)tableView:
(UITableView*)tableViewcellForRowAtIndexPath:
(NSIndexPath*)indexPath
return[selfsearchTableView:
mySearchDisplayController.searchResultsTableViewcellForRowAtIndexPath:
indexPath];
else
staticNSString*cellID=@"
search_cell"
;
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:
cellID];
if(cell==nil)
cell=[[UITableViewCellalloc]initWithStyle:
UITableViewCellStyleDefaultreuseIdentifier:
returncell;
//这里专门写UISearchDisplayController中searchResultsTableView的datasource和delegate
#pragmamark-UISearchDisplayController&
UITableViewDataSource&
dataSource
-(NSInteger)numberOfRowsWithSearchResultTableView
returnsuggestResults.count+1;
-(UITableViewCell*)searchTableView:
staticNSString*suggestId=@"
suggestCell"
suggestId];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
if(indexPath.row==suggestResults.count)
cell.textLabel.text=[NSLocalizedString(@"
Search:
"
查找:
)stringByAppendingString:
mySearchBar.text];
cell.textLabel.text=[suggestResultsobjectAtIndex:
indexPath.row];
UITableViewDelegate&
delegate
-(void)searchTableView:
(UITableView*)tableViewdidSelectRowAtIndexPath:
NSString*keyword=nil;
keyword=mySearchBar.text;
keyword=[suggestResultsobjectAtIndex:
[mySearchBarresignFirstResponder];
//开始我们的searchbardelegate,输入搜索内容,因为是Demo,所以我并没有搜索结果,这个大家根据需求自己实现吧
#pragmamark-UISearchBarDelegate
-(BOOL)searchBarShouldBeginEditing:
(UISearchBar*)searchBar
returnYES;
-(void)searchBarSearchButtonClicked:
{
-(void)searchBar:
(UISearchBar*)searchBartextDidChange:
(NSString*)searchText
[mySearchDisplayController.searchResultsTableViewreloadData];
//最后的环节了,这个地方的逻辑就会千变万化了
#pragmamark-UISearchDisplayDelegate
-(void)searchDisplayControllerDidBeginSearch:
(UISearchDisplayController*)controllerNS_DEPRECATED_IOS(3_0,8_0)
//开始搜索事件,设置searchResultsTableView的contentInset,否则位置会错误
mySearchDisplayController.searchResultsTableView.contentInset=UIEdgeInsetsMake(mySearchBar.frame.size.height,0,0,0);
-(void)searchDisplayController:
(UISearchDisplayController*)controllerdidLoadSearchResultsTableView:
(UITableView*)tableViewNS_DEPRECATED_IOS(3_0,8_0)
//加载searchResultsTableView完毕
-(void)searchDisplayControllerDidEndSearch:
//结束搜索事件,移除mySearchBar
[mySearchBarremoveFromSuperview];
//returnYEStoreloadtable.calledwhensearchstring/optionchanges.conveniencemethodsontopUISearchBardelegatemethods
-(BOOL)searchDisplayController:
(UISearchDisplayController*)controllershouldReloadTableForSearchString:
(NSString*)searchStringNS_DEPRECATED_IOS(3_0,8_0)
//是否需要刷新searchResultsTableView
if(@"
符合条件"
)
returnYES;
returnNO;
就到这里结束,是不是很容易?
这是第一次发布,如果做的不好,请大神指正,如果文笔不好(估计是肯定的),也请多指教,最后希望能多多帮助到大家。
Sely原创
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 OC iOS 如何 UINavigationBa
链接地址:https://www.bdocx.com/doc/16930627.html