效果图: api
首先在xib里面拖一个“Searchbar and Search Display”,记得还要放一个tableview在上面 spa
再于.h 文件中的iboutlet 变量 UISearchBar *searchBar 关联, 如何关联就很少说了,在xib里拖拽一个便可。 继承
下面是我设置searchbar的代码,放在了viewdidload里面: it
self.searchBar.delegate = self; io
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo; table
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; 变量
self.searchBar.placeholder = @"搜索"; select
self.searchBar.keyboardType = UIKeyboardTypeDefault; 搜索
[self.searchBar setShowsScopeBar:YES]; queue
[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;
}
仅供参考...