前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UISearchBar 搜索框

UISearchBar 搜索框

原创
作者头像
用户8983410
修改2021-10-29 10:50:44
1.4K0
修改2021-10-29 10:50:44
举报
代码语言:javascript
复制
///在 .h 写代理 <UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
///结合UITableView 展示了UISearchBar
_searchArray = [[NSMutableArray alloc] init];
    _dataArray = [[NSMutableArray alloc] initWithObjects:@"qq", @"tencent", @"NOKIA", @"samsung", @"google", @"apple", @"MicroSoft", @"htc", nil];

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
[_tableView release];

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 84)];
_tableView.tableHeaderView = searchBar;
//类型
//searchBar.barStyle = UIBarStyleBlack;
//占位符
searchBar.placeholder = @"请输入搜索内容";
//副标题
//searchBar.prompt = @"这是什么?";
//显示按钮
searchBar.showsBookmarkButton = YES;
searchBar.showsCancelButton = YES;
searchBar.showsSearchResultsButton = YES;
searchBar.showsScopeBar = YES;
[searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"a", @"b", @"c", @"d",nil]];
//设置代理
searchBar.delegate = self;




- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
NSArray* array = [NSArray arrayWithObjects:@"a", @"b", @"c", @"d",nil];
NSString* str = [array objectAtIndex:selectedScope];
searchBar.text = str;

}
//搜索

(void)searchBar:(UISearchBar )searchBar textDidChange:(NSString )searchText{
  //如果搜索栏为空,代表我们没有在搜索,tableView需要显示原数据。如果不为空,代表我们在搜索,tableView要显示搜索结果
  if (searchBar.text == nil || [searchBar.text isEqualToString:@""]) {

  _isSearch = NO;

} else {

  _isSearch = YES;
  [_searchArray removeAllObjects];
  for (NSString* str in _dataArray) {
      //判断str里面是否包含searchBar.text
      NSRange range = [str rangeOfString:searchBar.text];
      if (range.location != NSNotFound) {
          [_searchArray addObject:str];
      }
  }

}
  [_tableView reloadData];
}

(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
  [searchBar resignFirstResponder];
}


//tableView delegate

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  if (_isSearch) {

  return _searchArray.count;

}
  return _dataArray.count;
}

(UITableViewCell)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath{
  UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:indexPath.row%2==0?@"IDRed":@"IDBlue"];
  if (cell == nil) {

  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indexPath.row%2==0?@"IDRed":@"IDBlue"] autorelease];
  if (indexPath.row%2 == 0) {
      cell.contentView.backgroundColor = [UIColor redColor];
  } else {
      cell.contentView.backgroundColor = [UIColor blueColor];
  }

}
if (_isSearch) {

  cell.textLabel.text = [_searchArray objectAtIndex:indexPath.row];

} else {

  cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];

}



return cell;

}</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档