UISearchController

来源:互联网 发布:人在德国上淘宝 编辑:程序博客网 时间:2024/05/15 01:40
////  LJESearchController.m//  LJESearchController////  Created by lije on 15/6/29.//  Copyright (c) 2015年 lije. All rights reserved.//#import "LJESearchController.h"#define kRowHeight 60#define kSearchBarHeight 80#define kSearchBarWidth self.view.frame.size.width@interface LJESearchController ()<UISearchResultsUpdating> {    UITableView *_tableview;}@property (nonatomic, strong) NSArray *searchArr; // 保存搜索过的数据@property (nonatomic, strong) NSArray *dataSource; // 保存原有数据@property (nonatomic, strong) UISearchController *searchController;@end@implementation LJESearchControllerNSString * const CellIdentifier = @"CellIdentifier";- (void)viewDidLoad {    [super viewDidLoad];        self.title = @"Search";    [self configureLayout];}- (void)viewWillAppear:(BOOL)animated {    if (!_searchArr) {        return;    }        [_tableview reloadData];}- (void)configureLayout {    _tableview = [[UITableView alloc] initWithFrame:self.view.bounds                                              style:UITableViewStylePlain];    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];    _tableview.delegate = self;    _tableview.dataSource = self;    [self.view addSubview:_tableview];        // 创建搜索条    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];    // 设置字体渲染颜色    self.searchController.searchBar.tintColor = [UIColor orangeColor];    // 设置状态条颜色    self.searchController.searchBar.barTintColor = [UIColor orangeColor];    // 设置开始搜索的时候是否还显示背景颜色    self.searchController.dimsBackgroundDuringPresentation = YES;    // 让searchBar自适应屏幕大小    [self.searchController.searchBar sizeToFit];    // 设置显示搜索结果的控制器    self.searchController.searchResultsUpdater = self;    _tableview.tableHeaderView = self.searchController.searchBar;}#pragma mark - UITableviewDelegate- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return self.searchArr.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    return kRowHeight;}#pragma mark - UITableviewDataSource- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];        if (!cell) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];    }        if (indexPath.row < self.searchArr.count) {        cell.textLabel.text = self.searchArr[indexPath.row];    }        return cell;}#pragma mark - UISearchResultsUpdating- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@", searchController.searchBar.text];    self.searchArr = [NSMutableArray arrayWithArray:[self.dataSource filteredArrayUsingPredicate:predicate]];    [_tableview reloadData];}#pragma mark -  懒加载 runtime- (NSArray *)dataSource {    if (!_dataSource) {        self.dataSource = _dataSource = @[@"wangyu", @"zhangjing", @"mali"];    }        return _dataSource;}- (NSArray *)searchArr {    if (!_searchArr) {        // 如果searchArr为空则把原始的数据传给它        self.searchArr = self.dataSource;    }        return _searchArr;}@end

0 0