ios UITableView 搜尋

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   使用   io   for   資料   

自己實現 UITableView 搜尋,相對於使用 UISearchDisplayController 來說自己寫稍微麻煩了那麼一點點,但是更加靈活。主要就是用一個欄位區分出當前是搜尋還是非搜尋,然後 reload 相應的 data 就行了,和 UISearchDisplayController 的實現也很像,不過 UISearchDisplayController是兩個 tableview 切換,這裡我們是一個 tableview load 不同的資料。

關鍵代碼:

@interface MainTableViewController : UIViewController<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>{    UITableView *mytableView;    NSArray *data;    NSMutableArray *filterData;    BOOL isFiltered; // 標識是否正在搜素    UIView *mask;}
- (void)viewDidLoad{    [super viewDidLoad];    mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20)];    mytableView.dataSource = self;    mytableView.delegate = self;    [self.view addSubview:mytableView];        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width                                                                           , 44)];    searchBar.placeholder = @"搜尋";    searchBar.delegate = self;    mytableView.tableHeaderView = searchBar;        // 添加一層 mask    mask = [[UIView alloc] initWithFrame:CGRectMake(0, 20 + 44, self.view.frame.size.width, self.view.frame.size.height -20 -44)];    [self.view addSubview:mask];    mask.backgroundColor = [UIColor blackColor];    mask.alpha = 0;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    // 通過 isFiltered 區分出當前顯示的是搜尋結果集還是原結果集    if (isFiltered) {        return filterData.count;    }        return data.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellid = @"cellid";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];        if (cell==nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];    }        // 通過 isFiltered 區分出當前顯示的是搜尋結果集還是原結果集    if (isFiltered) {        cell.textLabel.text = filterData[indexPath.row];    }else{        cell.textLabel.text = data[indexPath.row];    }        return cell;}- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{    // 開始搜尋時彈出 mask 並禁止 tableview 點擊    NSLog(@"searchBarTextDidBeginEditing");    isFiltered = YES;    searchBar.showsCancelButton = YES;    mask.alpha = 0.3;    mytableView.allowsSelection = NO;    mytableView.scrollEnabled = NO;}- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{    NSLog(@"searchBarTextDidEndEditing");}- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{    NSLog(@"textDidChange");    if (searchText.length == 0) {        isFiltered = NO;        mask.alpha = 0.3;        mytableView.allowsSelection = NO;        mytableView.scrollEnabled = NO;        [mytableView reloadData];        return;    }        isFiltered = YES;    mask.alpha = 0;    mytableView.allowsSelection = YES;    mytableView.scrollEnabled = YES;        // 謂詞搜尋    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchText];    filterData =  [[NSMutableArray alloc] initWithArray:[data filteredArrayUsingPredicate:predicate]];    [mytableView reloadData];}- (void)searchBarCancelButtonClicked:(UISearchBar *) sb{    // 點擊 cancel 時去掉 mask ,reloadData    sb.text = @"";    [sb setShowsCancelButton:NO animated:YES];    mytableView.allowsSelection = YES;    mytableView.scrollEnabled = YES;    [sb resignFirstResponder];    mask.alpha = 0;        isFiltered = NO;    [mytableView reloadData];}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *text;        if (isFiltered) {        text = filterData[indexPath.row];    }else{        text = data[indexPath.row];    }        NSLog(@"you click index:%d  %@",indexPath.row,text);}

基於 ios7.1 布局寫了一個 demo: http://pan.baidu.com/s/1ntn0MeP

 

ios UITableView 搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.