美文网首页
iOS-视图-UISearchBar

iOS-视图-UISearchBar

作者: 我是谁重要吗 | 来源:发表于2018-03-29 21:21 被阅读9次
    ///在 .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;
    }
    

    相关文章

      网友评论

          本文标题:iOS-视图-UISearchBar

          本文链接:https://www.haomeiwen.com/subject/wzmvcftx.html