UISearchBar + Controller

//
//  UIView+FindChildView.h
//  XibDemo
//
//  Created by HELLO WORLD on 2019/9/11.
//  Copyright © 2019年 WaterProofer. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIView (FindChildView)
- (UIView*)subViewOfClassName:(NSString*)className;
@end

NS_ASSUME_NONNULL_END



//
//  UIView+FindChildView.m
//  XibDemo
//   搜索
//  Created by HELLO WORLD on 2019/9/11.
//  Copyright © 2019年 WaterProofer. All rights reserved.
//

#import "UIView+FindChildView.h"

@implementation UIView (FindChildView)
- (UIView*)subViewOfClassName:(NSString*)className {
    for (UIView* subView in self.subviews) {
        if ([NSStringFromClass(subView.class) isEqualToString:className]) {
            return subView;
        }
        
        UIView* resultFound = [subView subViewOfClassName:className];
        if (resultFound) {
            return resultFound;
        }
    }
    return nil;
}
@end
UIView+FindChildView
//
//  ZKSearchController.h
//  XibDemo
//
//  Created by HELLO WORLD on 2019/9/11.
//  Copyright © 2019年 WaterProofer. All rights reserved.
//

#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN

@protocol ZKSearchControllerDelegate <NSObject>
-(void)selectItemWithSearchData:(NSArray*)SearchData selectIndex:(NSInteger)index;
@end

@interface ZKSearchController : UITableViewController
@property(nonatomic,strong)NSArray* sourceData;
@property(nonatomic,weak)id<ZKSearchControllerDelegate>delegate;
@end

NS_ASSUME_NONNULL_END
ZKSearchController.h
//
//  ZKSearchController.m
//  XibDemo
//
//  Created by HELLO WORLD on 2019/9/11.
//  Copyright © 2019年 WaterProofer. All rights reserved.
//

#import "ZKSearchController.h"
#import "UIView+FindChildView.h"

@interface ZKSearchController ()<UISearchBarDelegate>
@property(nonatomic,strong)UISearchBar* searchBar;
@property(nonatomic,strong)NSMutableArray* searchArray;
@property(nonatomic,strong)NSMutableArray* mutArray;

@end

@implementation ZKSearchController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
    [txfSearchField becomeFirstResponder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _searchArray = [[NSMutableArray alloc] init];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.hidesBackButton = YES;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(btnClick)];
    rightItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    UIImageView* img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-80, 44)];
    img.userInteractionEnabled = YES;
    self.navigationItem.titleView = img;
    
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 5, [UIScreen mainScreen].bounds.size.width-100, 34)];
  //  _searchBar.searchBarStyle = UISearchBarStyleMinimal;//去掉边框
    _searchBar.delegate = self;
    _searchBar.placeholder = @"城市名/拼音";
//    _searchBar.barStyle = UIBarStyleDefault;
//    _searchBar.tintColor = [UIColor blackColor];//可设置光标颜色
//    _searchBar.barTintColor = [UIColor whiteColor];
//    _searchBar.backgroundColor = [UIColor whiteColor];
    _searchBar.clearsContextBeforeDrawing = YES;
   // _searchBar.backgroundImage = [UIImage imageNamed:@"navBackGround"];//navBackGround为白色背景图片
 //   [_searchBar setImage:[UIImage imageNamed:@"navBackGround"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];//设置左侧扩大镜图标
    UIView* backgroundView = [_searchBar subViewOfClassName:@"_UISearchBarSearchFieldBackgroundView"];
    backgroundView.layer.cornerRadius = 18.0f;
    backgroundView.clipsToBounds = YES;
    
    
//    UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
//    [txfSearchField setBackgroundColor:[UIColor whiteColor]];
//    [txfSearchField setLeftViewMode:UITextFieldViewModeNever];
//    [txfSearchField setRightViewMode:UITextFieldViewModeNever];
//    [txfSearchField setBackground:[UIImage imageNamed:@"searchbar_bgImg.png"]];
//    [txfSearchField setBorderStyle:UITextBorderStyleNone];
//    txfSearchField.layer.borderWidth = 8.0f;
//    txfSearchField.layer.cornerRadius = 10.0f;
//    txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
//    txfSearchField.clearButtonMode=UITextFieldViewModeNever;
    
     [img addSubview:_searchBar];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _searchArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    ProvinceModel* model =  _searchArray[indexPath.row];
    cell.textLabel.text = model.fullName;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if ([self.delegate respondsToSelector:@selector(selectItemWithSearchData:selectIndex:)]){
        [self.delegate selectItemWithSearchData:self.searchArray selectIndex:indexPath.row];
    }
    [self.navigationController popViewControllerAnimated:NO];
}
-(void)btnClick{
    [self.navigationController popViewControllerAnimated:NO];
}
#pragma mark UITableViewDelegate

//将要开始输入

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    searchBar.text = nil;
    
   // [_searchArray removeAllObjects];
    
    return YES;
    
}

//正在输入时

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    //精准搜索
    [_searchArray removeAllObjects];
    for(ProvinceModel* model in _mutArray){
        if (searchText.length<=model.fullName.length) {
            if ([[model.fullName substringToIndex:searchText.length] isEqualToString:searchText]&&![_searchArray containsObject:model]&&(searchText.length>0)) {
                 [_searchArray addObject:model];
            }
        }
    }
    [self.tableView reloadData];
    
//模糊搜索
//    [_searchArray removeAllObjects];
//    for(ProvinceModel* model in _mutArray){
//        NSRange range = [model.fullName rangeOfString:searchText];
//        if ((range.location!=NSNotFound)&&![_searchArray containsObject:model]&&(searchText.length>0)){
//            [_searchArray addObject:model];
//        }
//     }
//      [self.tableView reloadData];
    
}// called when text changes (including clear)
//搜索点击搜索按钮时

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //精准搜索
    [_searchArray removeAllObjects];
    for(ProvinceModel* model in _mutArray){
        if (searchBar.text.length<=model.fullName.length) {
            if ([[model.fullName substringToIndex:searchBar.text.length] isEqualToString:searchBar.text]&&![_searchArray containsObject:model]&&(searchBar.text.length>0)) {
                [_searchArray addObject:model];
            }
        }
    }
    [self.tableView reloadData];

}
-(void)setSourceData:(NSArray *)sourceData{
    _mutArray = [[NSMutableArray alloc] init];

    _sourceData = sourceData;
    for (NSArray* arr in _sourceData) {
        for (ProvinceModel* model in arr) {
            [_mutArray addObject:model];
        }
    }
}
@end
ZKSearchController.m

 

posted @ 2019-09-11 23:50  &#127810;浪迹天涯&#127810;  阅读(214)  评论(0编辑  收藏  举报