UISearchBar 搜索框

///在 .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;
}


转载于:https://my.oschina.net/liuchuanfeng/blog/201018

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值