iOS 搜索条使用详解

在ios开发中搜索条的使用挺常见的,不过之前一直没用到也没细细研究,最近做外包项目的时候刚好用到,在这里记录一下使用的过程,只要理解了原理,其实还是比较简单的!上传的图片有点大,刚好可以看清楚它的使用效果!

  我喜欢一步步的解析控件使用过程,其实真正的用心做一件事情是很享受的,虽然现在的社会都很注重效率和回报,这也是中国这种社会环境下难出大师级的人物的很重要的一个因素。扯得有点远了,只是希望国内开发者不要太急功近利,熟练应用本不是难事,一点点积累就好。下面开始搜索条
UISearchBar 和 UISearchDisplayController 的使用介绍。
  第一步:添加UISearchBarDelegate,UISearchDisplayDelegate 委托
  第二步:就是UISearchBar 和UISearchDisplayController的使用
 //搜索框
self.searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(10, 15, self.view.bounds.size.width-20, 45)]autorelease];
searchBar.placeholder=@"输入城市名或拼音";
searchBar.delegate = self;
searchBar.backgroundColor=[UIColor clearColor];
searchBar.backgroundImage=[UIImage imageNamed:@"搜索背景"];
searchBar.keyboardType=UIKeyboardTypeDefault;
myTableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;

self.searchdispalyCtrl = [[[UISearchDisplayController  alloc] initWithSearchBar:searchBar contentsController:self]autorelease];
//设置取消按钮文字颜色
searchdispalyCtrl.searchBar.tintColor=BB_Yanzheng_Color;
//search dispalyCtrl.active = NO;
//[searchdispalyCtrl.searchResultsTableView setHidden: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)

{

    return 1;

}

else

{

    return 26;

}

}

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

{

if (tableView == self.searchDisplayController.searchResultsTableView)

{

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

    return [searchResult count];

}

else

{

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

    {

        return 1;

    }

    if (isZiMu)

    {

        NSLog(@"zimugeshu:%@,%d",[zimuList objectAtIndex:section-4],[[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count]);

        return [[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count];

    }

   

    /*

    char mychar='a';

    //当前字母

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

    

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

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

    {

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

        return [[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count];

        

    }*/

   

    return 0;

    

}

}
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 45;
}
if (indexPath.section==0)
{
return 0;
}
if (indexPath.section==1||indexPath.section==2)
{
return 73;
}
if (indexPath.section==3)
{
//超过的话固定显示203
if (17+([remenCityList count]/3+1)
47+18<=223)
{
if ([remenCityList count]!=0&&[remenCityList count]%3==0)
{
return 17+([remenCityList count]/3)
47;
}
return 17+([remenCityList count]/3+1)*47;
}
return 223;
}
return 45;
}

  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
    return 0;
    }
    if (section!=0)
    {
    return 32;
    }
    return 0.01f;
    }

  • (CGFloat)tableView:(UITableView )tableView heightForFooterInSection:(NSInteger)section
    {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
    return 0.01;
    }
    return 0.01f;
    }
    这些个基本的高度和行数 模块信息,当然列表的显示更是必不可少
    -(UITableViewCell
    )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
    static NSString *simpleTableIdentifier = @"RecipeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]autorelease];
    }
    else
    {
    for(UIView *view in cell.contentView.subviews)
    {
    [view removeFromSuperview];
    }
    }
    //cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];
    //[cell.textLabel setTextColor:BB_Red_Color];
    //NSLog(@"zheli:%@,%d,%@",[searchResult objectAtIndex:indexPath.row],indexPath.row,self.searchDisplayController.searchResultsTableView);

      //城市名称
      UILabel *labLeft = [[[UILabel alloc]init]autorelease];
      labLeft.backgroundColor = [UIColor clearColor];
      labLeft.textAlignment = NSTextAlignmentLeft;
      labLeft.font = [UIFont systemFontOfSize:15];
      labLeft.textColor = [UIColor blackColor];
      labLeft.frame = CGRectMake(15, 15, 200, 15);
      labLeft.text=[searchResult objectAtIndex:indexPath.row];
      [cell.contentView addSubview:labLeft];
      return cell;
    

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

pragma mark - UISearchDisplayDelegate

  • (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{

[self filterContentForSearchText:searchString

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



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

return YES;

}

-(void)filterContentForSearchText:(NSString)searchText scope:(NSString)scope

{

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

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

[tempResults removeAllObjects];

[tempResultsId removeAllObjects];



NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;

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

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

{

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

    

    NSRange storeRange = NSMakeRange(0, storeString.length);

    

    NSRange foundRange = [storeString rangeOfString:searchText options:searchOptions range:storeRange];

   

    

    if (foundRange.length)

    {

         NSLog(@"foundRange:%d,%d",foundRange.length,foundRange.location);

        [tempResults addObject:storeString];

        [tempResultsId addObject:[[storeResults objectAtIndex:i]objectForKey:@"id"]];

    }

    

    

    NSLog(@"wodejieguo:%@,%@",[[storeResults objectAtIndex:i]objectForKey:@"id"],storeString);

}

//清空搜索内容Id

[self.searchResultId removeAllObjects];

[self.searchResult removeAllObjects];

[self.searchResultId addObjectsFromArray:tempResultsId];

[self.searchResult addObjectsFromArray:tempResults];

NSLog(@"searchResult:%@,%@",searchResult,searchResultId);

}
说道这里关于搜索条的使用已经基本完成,当然搜索列表选中跳页可以借助自定义委托来实现,这里就不多少了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容