美文网首页
UISearchBar

UISearchBar

作者: 七里田间的守望者 | 来源:发表于2016-12-23 17:06 被阅读23次
    ###声明
    @interface NZFeedBackHousingViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>
    {
        UISearchBar* housingSearchBar;//搜索条
        UITableView* housingTableV;//显示列表的tableview
        NSArray * tableData;//保存原始数据表格
        NSArray * searchData;//保存搜索结果的数据
        BOOL isSearch;//是否搜索
    }
    @end
    
    ###搭建UI
    - (void)viewDidLoad
     {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.automaticallyAdjustsScrollViewInsets = NO;
    
      //搜索的全部数据 -- 当数据为字符串的时候
        tableData = @[@"a小去1",@"a小去2",@"a小去3",@"a小去4",@"a小去5",@"a小去6",@"a小去7",@"a小去8",@"a小去9",@"a小去10",@"a小去11",];
        
        //查询框
        [self layoutHousingSearchUI];
        //查询小区列表
        [self layoutHousingNameUI];
    
        isSearch = NO;
    
    }
    
    #pragma mark UI
    - (void) layoutHousingSearchUI
    {
        
        housingSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(SP(10), 0, SCREEN_WIDTH -SP(20), SP(30))];
        housingSearchBar.placeholder = @"搜索小区名称";
        housingSearchBar.tintColor = kAppThemeColor;
        housingSearchBar.searchBarStyle = UISearchBarStyleMinimal;
    //    housingSearchBar.showsCancelButton = YES;//右边的取消按钮
        housingSearchBar.delegate = self;
    }
    
    - (void) layoutHousingNameUI
    {
        housingTableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        housingTableV.delegate = self;
        housingTableV.dataSource = self;
        housingTableV.showsVerticalScrollIndicator = NO;
        housingTableV.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:housingTableV];
        //设置tableview的headerView为搜索框
        housingTableV.tableHeaderView = housingSearchBar;
    }
    
    

    以上代码是搭建UI

    下面进入重点部分 -- 搜索框的代理方法

    
    #pragma mark - UISearchBarDelegate
    // UISearchBarDelegate定义的方法,用户单击取消按钮时激发该方法
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
        // 取消搜索状态
        isSearch = NO;
        [housingTableV reloadData];
        [self.view endEditing:YES];
    }
    // UISearchBarDelegate定义的方法,当搜索文本框内文本改变时激发该方法
    - (void)searchBar:(UISearchBar *)searchBar
        textDidChange:(NSString *)searchText
    {
        // 调用filterBySubstring:方法执行搜索
        [self filterBySubstring:searchText];
    }
    
    // UISearchBarDelegate定义的方法,用户单击虚拟键盘上Search按键时激发该方法
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    {
        // 调用filterBySubstring:方法执行搜索
        [self filterBySubstring:searchBar.text];
        // 放弃作为第一个响应者,关闭键盘
        [searchBar resignFirstResponder];
    }
    
    - (void)filterBySubstring:(NSString*) subStr
    {
        // 设置为搜索状态
        isSearch = YES;
        // 定义搜索谓词
        NSPredicate* pred = [NSPredicate predicateWithFormat:
                             @"SELF CONTAINS[c] %@" , subStr];
        //当数据为实体对象时
       /* NSMutableArray * tempArray = [NSMutableArray array];
        for (NZChooseHousingModel * model in tableData) {
            if([pred evaluateWithObject:model.nameStr])
            {
                [tempArray addObject:model];
            }
        }
        // 使用谓词过滤NSArray [tableData filteredArrayUsingPredicate:pred]
        searchData = tempArray;
    */
    
        //当数据为字符串的时候
        [tableData filteredArrayUsingPredicate:pred]
    
    
        // 让表格控件重新加载数据
        [housingTableV reloadData];
    }
    

    tableview的代理方法

    
    #pragma mark delegate datasource
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    
        if (isSearch)
        {
            return searchData.count;
            
        }else
        {
            return tableData.count;
        }
    
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return SP(40);
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString* housingIdentifier = @"Identifier";
        UITableViewCell* myBulidCell = [tableView dequeueReusableCellWithIdentifier:housingIdentifier];
        if (!myBulidCell) {
            myBulidCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:housingIdentifier];
        }
        
        if (isSearch) {
            NZChooseHousingModel * model = [searchData objectAtIndex:indexPath.row];
            myBulidCell.textLabel.text = model.nameStr;
            myBulidCell.detailTextLabel.text = model.addressStr;
        }else{
            NZChooseHousingModel * model = [tableData objectAtIndex:indexPath.row];
            myBulidCell.textLabel.text = model.nameStr;
            myBulidCell.detailTextLabel.text = model.addressStr;
        }
    
        return myBulidCell;
        
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        NZChooseHousingModel * model = nil;
        if (isSearch) {//获得选中的搜索内容
            model = searchData[indexPath.row];
        }else{
            model = tableData[indexPath.row];
        }
    
    }
    
    7A92EBF0-39C5-4C85-8261-58BB29A705BC.png

    相关文章

      网友评论

          本文标题:UISearchBar

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