关于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.keyboardType =  UIKeyboardTypeDefault;

    [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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值