【移动应用开发技术】iOS 搜索条使用详解_第1页
【移动应用开发技术】iOS 搜索条使用详解_第2页
【移动应用开发技术】iOS 搜索条使用详解_第3页
【移动应用开发技术】iOS 搜索条使用详解_第4页
【移动应用开发技术】iOS 搜索条使用详解_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】iOS搜索条使用详解

在ios开发中搜索条的使用挺常见的,不过之前一直没用到也没细细研究,最近做外包项目的时候刚好用到,在这里记录一下使用的过程,只要理解了原理,其实还是比较简单的!上传的图片有点大,刚好可以看清楚它的使用效果!我喜欢一步步的解析控件使用过程,其实真正的用心做一件事情是很享受的,虽然现在的社会都很注重效率和回报,这也是中国这种社会环境下难出大师级的人物的很重要的一个因素。扯得有点远了,只是希望国内开发者不要太急功近利,熟练应用本不是难事,一点点积累就好。下面开始搜索条

UISearchBar

UISearchDisplayController

的使用介绍。第一步:添加UISearchBarDelegate,UISearchDisplayDelegate委托第二步:就是UISearchBar和UISearchDisplayController的使用//搜索框

self.searchBar=[[[UISearchBaralloc]initWithFrame:CGRectMake(10,15,self.view.bounds.size.width-20,45)]autorelease];

searchBar.placeholder=@"输入城市名或拼音";

searchBar.delegate=self;

searchBar.backgroundColor=[UIColorclearColor];

searchBar.backgroundImage=[UIImagep_w_picpathNamed:@"搜索背景"];

searchBar.keyboardType=UIKeyboardTypeDefault;

myTableView.tableHeaderView=searchBar;

searchBar.autocorrectionType=UITextAutocorrectionTypeNo;

searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;

self.searchdispalyCtrl=[[[UISearchDisplayController

alloc]initWithSearchBar:searchBarcontentsController:self]autorelease];

//设置取消按钮文字颜色

searchdispalyCtrl.searchBar.tintColor=BB_Yanzheng_Color;

//searchdispalyCtrl.active=NO;

//[searchdispalyCtrl.searchResultsTableViewsetHidden:NO];

searchdispalyCtrl.delegate=self;

searchdispalyCtrl.searchResultsDelegate=self;

searchdispalyCtrl.searchResultsDataSource=self;这里面有两个点需要注意一下,一个是

myTableView.tableHeaderView=searchBar;这里是将searchbar放在对应列表的第一个section之中,而且section的高度不用设置,当然可以通过addsubview添加这种方式会产生一种移位,建议默认的方式比较好些!另一点是UISearchDisplayController是对searchbar的进一步封装,里面的东西确实简化了好多,可以采用这种方法设置取消按钮文本的颜色searchdispalyCtrl.searchBar.tintColor=BB_Yanzheng_Color;第三步:相关委托的使用我们通过实验发现点击searchbar的输入框会弹出一个独立的tableview视图,并显示一个取消按钮,搜索的数据显示在这个独立的视图列表中。而这个视图列表和普通的列表用法一样,需要设置

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

if(tableView==self.searchDisplayController.searchResultsTableView)

{

return1;

}

else

{

return26;

}

}

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

{

if(tableView==self.searchDisplayController.searchResultsTableView)

{

NSLog(@"我去:%@",searchResult);

return[searchResultcount];

}

else

{

if(section==0||section==1||section==2||section==3)

{

return1;

}

if(isZiMu)

{

NSLog(@"zimugeshu:%@,%d",[zimuListobjectAtIndex:section-4],[[cityGroupListobjectForKey:[zimuListobjectAtIndex:section-4]]count]);

return[[cityGroupListobjectForKey:[zimuListobjectAtIndex:section-4]]count];

}

/*

charmychar='a';

//当前字母

NSString*zimu=[NSStringstringWithFormat:@"%c",mychar-32];

//NSString*zimu=[NSStringstringWithFormat:@"%c",c];

if(![zimuisEqualToString:@"i"]&&![zimuisEqualToString:@"o"]&&![zimuisEqualToString:@"u"]&&![zimuisEqualToString:@"v"])

{

NSLog(@"zimugeshu:%@,%d",zimu,[[cityGroupListobjectForKey:zimu]count]);

return[[cityGroupListobjectForKey:[zimuListobjectAtIndex:section-4]]count];

}*/

return0;

}

}-(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{

if(tableView==self.searchDisplayController.searchResultsTableView)

{

return45;

}

if(indexPath.section==0)

{

return0;

}

if(indexPath.section==1||indexPath.section==2)

{

return73;

}

if(indexPath.section==3)

{

//超过的话固定显示203

if(17+([remenCityListcount]/3+1)*47+18<=223)

{

if([remenCityListcount]!=0&&[remenCityListcount]%3==0)

{

return17+([remenCityListcount]/3)*47;

}

return17+([remenCityListcount]/3+1)*47;

}

return223;

}

return45;}-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section{

if(tableView==self.searchDisplayController.searchResultsTableView)

{

return0;

}

if(section!=0)

{

return32;

}

return0.01f;}-(CGFloat)tableView:(UITableView*)tableViewheightForFooterInSection:(NSInteger)section{

if(tableView==self.searchDisplayController.searchResultsTableView)

{

return0.01;

}

return0.01f;}这些个基本的高度和行数模块信息,当然列表的显示更是必不可少-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{

if(tableView==self.searchDisplayController.searchResultsTableView)

{

staticNSString*simpleTableIdentifier=@"RecipeCell";

UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:simpleTableIdentifier];

if(cell==nil)

{

cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:simpleTableIdentifier]autorelease];

}

else

{

for(UIView*viewincell.contentView.subviews)

{

[viewremoveFromSuperview];

}

}

//cell.textLabel.text=[searchResultobjectAtIndex:indexPath.row];

//[cell.textLabelsetTextColor:BB_Red_Color];

//NSLog(@"zheli:%@,%d,%@",[searchResultobjectAtIndex:indexPath.row],indexPath.row,self.searchDisplayController.searchResultsTableView);

//城市名称

UILabel*labLeft=[[[UILabelalloc]init]autorelease];

labLeft.backgroundColor=[UIColorclearColor];

labLeft.textAlignment=NSTextAlignmentLeft;

labLeft.font=[UIFontsystemFontOfSize:15];

labLeft.textColor=[UIColorblackColor];

labLeft.frame=CGRectMake(15,15,200,15);

labLeft.text=[searchResultobjectAtIndex:indexPath.row];

[cell.contentViewaddSubview:labLeft];

returncell;

}}上面列出的只是显示信息,真正数据的搜索要依赖于下面的委托方法实现

#pragmamark-UISearchDisplayDelegate

-(BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchString:(NSString*)searchString

{

[selffilterContentForSearchText:searchString

scope:[self.searchBarscopeButtonTitles][self.searchBar.selectedScopeButtonIndex]];

NSLog(@"woshuoba:%@,%@,%d,%@",searchString,self.searchBar.scopeButtonTitles,self.searchBar.selectedScopeButtonIndex,[self.searchBarscopeButtonTitles][self.searchBar.selectedScopeButtonIndex]);

returnYES;

}

-(void)filterContentForSearchText:(NSString*)searchTextscope:(NSString*)scope

{

NSMutableArray*tempResults=[[[NSMutableArrayarray]init]autorelease];

NSMutableArray*tempResultsId=[[[NSMutableArrayarray]init]autorelease];

[tempResultsremoveAllObjects];

[tempResultsIdremoveAllObjects];

NSUIntegersearchOptions=NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch;

NSLog(@"sousuo:%@,%@,%@",searchText,scope,storeResults);

for(inti=0;i<storeResults.count;i++)

{

NSString*storeString=[[storeResultsobjectAtIndex:i]objectForKey:@"name"];

NSRangestoreRange=NSMakeRange(0,storeString.length);

NSRangefoundRange=[storeStringrangeOfStrin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论