您的位置:首页 > 产品设计 > UI/UE

UISearchDisplayController(iOS < 8.0)实例应用实现搜索功能和VC展示效果, 以及代理方法的介绍

2015-09-13 10:53 1041 查看

创建UITableViewController添加到NavigationController中 并作为根视图显示

[code]/* .m文件中得延展部分 */
@interface TableViewController ()
/* 添加UISearchDisplayController属性方便全局调用 */
@property (nonatomic, retain) UISearchDisplayController *searchDC;
/* 数据筛选结果数组 */
@property (nonatomic, retain) NSMutableArray *resultArr;
/* 数据源数组 */
@property (nonatomic, retain) NSMutableArray *arr;

@end


主程序部分

[code]- (void)viewDidLoad {
    [super viewDidLoad];
    /* 创建数据源 */
    self.arr = [NSMutableArray arrayWithObjects:@"class 1", @"class 2", @"class 3", @"class 4", @"class 4", @"lanou", nil];
    /**
     * 需要初始化UISearchBar, 添加到某个父视图, 将对象提供给displayController管理
     * displayController会初始化搜索结果的TableView, 我们只需要指定一个负责显示的UIViewController 和 设置代理方法即可
     */
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
    /* tableView的属性, 将UISearchBar添加到tableView 第一个分区的headerView位置 */
    [self.tableView setTableHeaderView:search];
    /* 初始化带有两个参数的UISearchDisplayController对象 */
    self.searchDC = [[UISearchDisplayController alloc] initWithSearchBar:search contentsController:self]; /**< @prarm 2: 负责展示搜索结果的ViewController*/
    /* 设置代理 */
    /* self.tableView本身的代理也是自己 所以使用时需要判断是哪个tableView */
    _searchDC.searchResultsDataSource = self;
    _searchDC.searchResultsDelegate = self;

    [search release];
}


创建的UITableViewController根视图自带的代理方法, 简单设置区分调用的是tableView本身数据列表 还是显示搜索结果列表

[code]- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    /* 使用tableView区分搜索结果列表和自己的tableView */
    if (tableView == self.tableView) {
        return 1;
    }
    return 1;
}
/* 因为源数据列表和显示结果列表section中的行数不同所以选择在刷新行数的时候筛选数据 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    /* 判断是哪个tableView */
    if (tableView == self.tableView) {
        return self.arr.count;
    }
    /* 每次输入字符改变时系统会刷新一次 重新调用这些方法与tableView reaload 效果一样
     */
     /* 创建谓词对象 */
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"self contains [cd] %@", self.searchDC.searchBar.text];
    /* 满足要求的数据添加到筛选结果数据数组 */
    self.resultArr = (NSMutableArray *)[self.arr filteredArrayUsingPredicate:pred];
    return self.resultArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:CellIdentifier] autorelease];
    }
    if (tableView == self.tableView) {
        cell.textLabel.text = [self.arr objectAtIndex:indexPath.row];

    } else {
        cell.textLabel.text = [self.resultArr objectAtIndex:indexPath.row];
    }
    return cell;
}


内存管理

[code]- (void)dealloc {
    [_resultArr release];
    [_arr release];
    [_searchDC release];
    [super dealloc];
}


UISearchDisplayDelegate

关于搜索的设置

[code]// when we start/end showing the search UI
- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller;
- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller;
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller;
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller;


加载结果列表

[code]// called when the table is created destroyed, shown or hidden. configure as necessary.
/* 已经加载搜索结果列表 */
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView;
 /* 将要不加载搜索结果列表 */
- (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView;


关于TableView展示和隐藏搜索结果列表

[code]// called when table is shown/hidden
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView;
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView;
- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView;
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView;


搜索框输入内容时同时加载TableView的显示

[code]- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption;


注1 : UISearchDisplayController搜索显示控制器

[code]/**
 * 收缩显示控制器, 继承于NSObject, 费视图控制器的一个工具类
 * 提供控制一个searchBar需要创建, 添加到指定的视图, 提供给搜索控制器
 * 搜索结果tableView, 我们需要指定一个UIViewController负责显示, 需要指定代理 delegate, dataSource
 * 设置代理, 实现代理方法控制搜索功能。
 */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: