• iOS 用UISearchDisplayController实现查找功能


      UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用

      示例代码如下:

      1 //
      2 //  WKRootViewController.m
      3 //  表格视图的搜索功能
      4 //
      5 //  Created by student on 14-10-20.
      6 //  Copyright (c) 2014年 wukong. All rights reserved.
      7 //
      8 
      9 #import "WKRootViewController.h"
     10 
     11 @interface WKRootViewController ()
     12 
     13 @property (strong, nonatomic) NSMutableArray* dataSource;
     14 
     15 @property (strong, nonatomic)NSMutableArray* resultArrat;
     16 
     17 
     18 @end
     19 
     20 @implementation WKRootViewController
     21 {
     22     //用于加载数据源的表视图
     23     UITableView *_tableView;
     24     
     25     UISearchBar *_searchBar;
     26     
     27     UISearchDisplayController *_searchControl;
     28 }
     29 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
     30 {
     31     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     32     if (self) {
     33         // Custom initialization
     34     }
     35     return self;
     36 }
     37 
     38 - (void)viewDidLoad
     39 {
     40     [super viewDidLoad];
     41     
     42     [self createUI];
     43     [self createDataSource];
     44     // Do any additional setup after loading the view.
     45 }
     46 
     47 - (void)createDataSource
     48 {
     49     _dataSource = [[NSMutableArray alloc] init];
     50     _resultArrat = [[NSMutableArray alloc] init];
     51     for (int i = 'A'; i <= 'z'; i++) {
     52         NSMutableArray *section = [[NSMutableArray alloc] init];
     53         for (int j = 1; j <= 10; j++) {
     54             NSString *str = [NSString stringWithFormat:@"%c-%d", i, j];
     55             [section addObject:str];
     56         }
     57         [_dataSource addObject:section];
     58     }
     59 }
     60 
     61 #pragma mark- UITableViewDataSource
     62 
     63 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     64 {
     65     //判断当前展示的表格
     66     if (tableView != _tableView)
     67         return 1;
     68     return _dataSource.count;
     69 }
     70 
     71 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     72 {
     73     if (tableView != _tableView) {
     74         return _resultArrat.count;
     75     }
     76     return [[_dataSource objectAtIndex:section] count];
     77 }
     78 
     79 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     80 {
     81     [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
     82     
     83     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     84     if (tableView != _tableView) {
     85         cell.textLabel.text = [_resultArrat objectAtIndex:indexPath.row];
     86     }else{
     87         cell.textLabel.text = [[_dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
     88     }
     89     return cell;
     90 }
     91 
     92 - (void)didReceiveMemoryWarning
     93 {
     94     [super didReceiveMemoryWarning];
     95     // Dispose of any resources that can be recreated.
     96 }
     97 
     98 
     99 #pragma mark - CreateUI
    100 - (void)createUI
    101 {
    102     _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 30, 300, 440) style:UITableViewStylePlain];
    103     _tableView.delegate = self;
    104     _tableView.dataSource = self;
    105     [self.view addSubview:_tableView];
    106     
    107     _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
    108     _searchBar.searchBarStyle = UISearchBarStyleMinimal;
    109     _searchBar.delegate = self;
    110     [_tableView setTableHeaderView:_searchBar];
    111     /*
    112      第一个参数:用于输入搜索内容的UISearchBar对象
    113      第二个参数:提供给我的表格视图数据源的控制器对象,这个对象必须是实现了表格的两个协议
    114      */
    115     _searchControl = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    116 //    _searchControl.searchResultsTableView
    117 //    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    118 //    label.backgroundColor =[UIColor redColor];
    119 //    [_searchControl.searchResultsTableView setTableHeaderView:label];
    120     //设置_searchControl自带的表格视图的委托对象
    121     [_searchControl setSearchResultsDataSource:self];
    122     [_searchControl setSearchResultsDelegate:self];
    123 }
    124 
    125 #pragma mark -UISearchBarDelegate
    126 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    127 {
    128     [_resultArrat removeAllObjects];
    129     NSString *str = [NSString stringWithFormat:@"*%@*", searchText];
    130     NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", str];
    131     for (NSMutableArray *arr in _dataSource) {
    132         for (NSString *str in arr) {
    133             if ([pred evaluateWithObject:str]) {
    134                 [_resultArrat addObject:str];
    135             }
    136         }
    137     }
    138 }
    139 @end
  • 相关阅读:
    Lock wait timeout exceeded; try restarting transaction linux设置mysql innodb_lock_wait_timeout
    用NaviCat创建存储过程批量添加测试数据
    mysql存储过程语法及实例
    mysql中迅速插入百万条测试数据的方法
    mysql学习之通过文件创建数据库以及添加数据
    有用的网站集合
    VMware Workstation虚拟磁盘文件备份或移植
    CoreData修改了数据模型报错 The model used to open the store is incompatible with the one used to create the store
    iOS中自定义UITableViewCell的用法
    golang make()的第三个参数
  • 原文地址:https://www.cnblogs.com/pretty-guy/p/4063539.html
Copyright © 2020-2023  润新知