关于searchbar的ScopeButton - 从无到有,纪录编程中的点点滴滴

效果图:

关于searchbar的ScopeButton - 从无到有,纪录编程中的点点滴滴

 首先在xib里面拖一个“Searchbar and Search Display”,记得还要放一个tableview在上面

再于.h 文件中的iboutlet 变量 UISearchBar *searchBar 关联, 如何关联就不多说了,在xib里拖拽一个即可。

下面是我设置searchbar的代码,放在了viewdidload里面:

    self.searchBar.delegate = self;

    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

    self.searchBar.placeholder = @"搜索";

    self.searchBar.keyboardTypeUIKeyboardTypeDefault;

    [self.searchBar setShowsScopeBar:YES];

    [self.searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"分类",@"价格",@"销量", nil]];

    self.searchBar.selectedScopeButtonIndex = 0;

继承 UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate。(这里说一句,我在没有添加tableview之前,似乎没有效果出来的,具体什么原因,猜测可能要与tableview一起使用的吧)

下面是我实现delegate的具体方法:

-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{

    NSLog(@"%d",selectedScope);

}


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

    return 5;

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 44;

}


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"mycell"] autorelease];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    

    return cell;

}

仅供参考...

转载于:https://my.oschina.net/u/237983/blog/108930