IOS 用UISearchController搜索Tableview

来源:互联网 发布:淘宝如何查看店铺粉丝 编辑:程序博客网 时间:2024/05/17 04:31

原创Blog,转载请注明出处
blog.csdn.net/hello_hwc


前言:
本文是Demo的第一阶段,简单的讲解如何使用UISearchController进行tableview搜索。下一篇文章会在这个基础上深一步讲解更多的代理函数和Tableview的使用。本文源代码只提供OC版本,下一篇我会提供OC和Swift两个版本。


Demo效果
最简单的按照前缀来搜索


一 UISearchController

这是一个提供了SearchBar的Controller,继承自UIViewController。使用它提供的SearchBar做为用户输入,使用它的代理函数来实时处理用户输入的事件,并且把结果返回给resultViewController。

初始化
这里的searchResultsController负责显示搜索后的结果

- (instancetype)initWithSearchResultsController:(UIViewController *)searchResultsController

SearchBar

@property(nonatomic, retain, readonly) UISearchBar *searchBar

searchResultsUpdater

负责更新resultViewController的对象,必须实现UISearchResultsUpdating协议

@property(nonatomic, assign) id< UISearchResultsUpdating > searchResultsUpdater

dimsBackgroundDuringPresentation

展示的时候,背景变暗。如果是在同一个view 中,则设为NO。默认为YES。

@property(nonatomic, assign) BOOL dimsBackgroundDuringPresentation

hidesNavigationBarDuringPresentation

是否隐藏导航栏,默认为YES。

@property(nonatomic, assign) BOOL hidesNavigationBarDuringPresentation

二 UISearchResultsUpdating协议

这个协议只有一个方法

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

每次当SearchBar的成为firstResponser,或者searchBar的内容改变的时候,就会调用。


三 代码讲解

首先,因为searchController有一个resultController,这里用tableview的方式显示result,所以定义一个SearchResultViewController来显示搜索的结果。
代码如下
很简单的tableviewController,接口是一个数组,做为数据源。

#import <UIKit/UIKit.h>@interface SearchResultViewController : UITableViewController@property (strong,nonatomic)NSArray * dataArray;@end
#import "SearchResultViewController.h"@implementation SearchResultViewController-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];    if (cell ==nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];    }    cell.textLabel.text = self.dataArray[indexPath.row];    return cell;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.dataArray.count;}@end

其次,主tableviewController添加属性

@interface SearchTableViewController()<UISearchBarDelegate,UISearchResultsUpdating>@property (strong,nonatomic)NSArray * dataArray;//数据源@property (strong,nonatomic)UISearchController * searchcontroller;//@property (strong,nonatomic)SearchResultViewController * resultViewController;//搜索结果显示@end

实现
在ViewDidLoad里初始化

-(void)viewDidLoad{    self.dataArray = @[@"jack",@"lucy",@"tom",@"truck",@"lily"];    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];//去除多余行    self.resultViewController = [[SearchResultViewController alloc] init];    self.searchcontroller = [[UISearchController alloc] initWithSearchResultsController:self.resultViewController];    self.searchcontroller.searchBar.delegate = self;    [self.searchcontroller.searchBar sizeToFit];    self.searchcontroller.searchResultsUpdater = self;    self.searchcontroller.dimsBackgroundDuringPresentation = NO;    self.definesPresentationContext = YES;    self.tableView.tableHeaderView = self.searchcontroller.searchBar;}

tableView的DataSource

#pragma mark - tableview datasource-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];    cell.textLabel.text = self.dataArray[indexPath.row];    return cell;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.dataArray.count;}-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{}

代理方法,处理搜索的过程

#pragma mark - search bar delegate-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{    [searchBar resignFirstResponder];}#pragma mark - UISearchResultUpdating-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{    NSString * searchtext = searchController.searchBar.text;    NSArray * searchResults = [self.dataArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {        BOOL result = NO;        if ([(NSString *)evaluatedObject hasPrefix:searchtext]) {            result = YES;        }        return result;    }]];    SearchResultViewController *tableController = (SearchResultViewController *)self.searchcontroller.searchResultsController;    tableController.dataArray = searchResults;    [tableController.tableView reloadData];}

理解,UISearchController是在原始的tableview上又添加了View,结构关系如图
这里写图片描述


BTY,回家了我上传源代码

2 0
原创粉丝点击