UISearchBarController

//  Created by Rain Dou on 15/5/18.
//  Copyright © 2015年 634778311 All rights reserved.

#import "DDYSearchVC.h"

@interface DDYSearchVC ()

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *tempsArray;
@property (nonatomic, strong) UIButton       *testLab;

@end

@implementation DDYSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.bounces = NO;
    self.definesPresentationContext = YES;
    [self layoutSearchController];
    [self layoutTestView];
    self.tableView.tableFooterView = [UIView new];
    self.edgesForExtendedLayout = UIRectEdgeTop;
    [self loadData];
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)tempsArray {
    if (!_tempsArray) {
        _tempsArray = [NSMutableArray array];
    }
    return _tempsArray;
}

- (void)layoutTestView {
    _testLab = [UIButton buttonWithType:UIButtonTypeCustom];
    [_testLab addTarget:self action:@selector(openMobileContact) forControlEvents:UIControlEventTouchUpInside];
    [_testLab setTitle:@"Add Friends" forState:UIControlStateNormal];
    [_testLab setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_testLab setImage:[UIImage imageNamed:@"addFriends"] forState:UIControlStateNormal];
    _testLab.frame = CGRectMake(DDYSCREENW/2.0-100, 80, 200, 200);
    [self.tableView addSubview:_testLab];
    _testLab.titleLabel.font = [UIFont systemFontOfSize:14];
    _testLab.hidden = YES;
    [_testLab DDYStyle:DDYStyleImgTop padding:8.0];
}
- (void)openMobileContact{  
}

- (void)layoutSearchController {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.placeholder = @"搜索";
    self.searchController.dimsBackgroundDuringPresentation = NO;
     self.searchController.hidesNavigationBarDuringPresentation = NO;
    
    self.searchController.searchBar.frame = CGRectMake(0, 0, DDYSCREENW, 44);
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.searchController.searchBar.delegate = self;
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return !self.searchController.active ? self.dataArray.count : self.tempsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = !self.searchController.active ? self.dataArray[indexPath.row] : self.tempsArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    [self filterContentForSearchText:self.searchController.searchBar.text];
    [self.tableView reloadData];
}

#pragma mark - Private Method
#pragma mark 源字符串内容是否包含或等于要搜索的字符串内容
- (void)filterContentForSearchText:(NSString *)searchText{
    if (searchText.length==0 && self.searchController.active) {
        _testLab.hidden = NO;
    }
    else{
        _testLab.hidden = YES;
    }
    NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
    [self.tempsArray removeAllObjects];
    for (int i = 0; i < self.dataArray.count; i++) {
        NSString *title = self.dataArray[i];
        NSRange storeRange = NSMakeRange(0, title.length);
        NSRange foundRange = [title rangeOfString:searchText options:searchOptions range:storeRange];
        if (foundRange.length) {
            [self.tempsArray addObject:self.dataArray[i]];
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 防止点击cell后仍有颜色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UISearchBarDelegate
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    _testLab.hidden = YES;
}

// 不用这种形式,可能有时候第一次展示没改变颜色,只改变了文字
//- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
//    searchBar.showsCancelButton = YES;
//    _testLab.hidden = NO;
//    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
//        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
//            UIButton *cancelBtn =(UIButton *)view;
//            cancelBtn.titleLabel.font = DDYFont(17);
//            [cancelBtn setTitle:DDYLocalStr(@"Cancel") forState:UIControlStateNormal];
//            [cancelBtn setTitleColor:FF_MAIN_COLOR forState:UIControlStateNormal];
//            [cancelBtn setTitleColor:FF_MAIN_COLOR forState:UIControlStateHighlighted];
//        }
//    }
//    return YES;
//}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    _testLab.hidden = NO;
    // 改变取消字体
    _searchController.searchBar.showsCancelButton = YES;
    UIButton *cancelBtn = [_searchController.searchBar valueForKey:@"cancelButton"];
    [cancelBtn setTitleColor:FF_MAIN_COLOR forState:UIControlStateNormal];
    [cancelBtn setTitle:DDYLocalStr(@"Cancel") forState:UIControlStateNormal];
    return YES;
}

- (void)loadData {
    // 网络请求
    [self.tableView reloadData];
}

@end

你可能感兴趣的:(UISearchBarController)