#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
#import "ZKSearchController.h" #import "UIView+FindChildView.h" @interface ZKSearchController ()<UISearchBarDelegate> @property(nonatomic,strong)UISearchBar* searchBar; @property(nonatomic,strong)NSMutableArray* searchArray; @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; 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)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (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"]; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self.delegate selectItemWithSearchData:self.searchArray selectIndex:indexPath.row]; [self.navigationController popViewControllerAnimated:NO]; } -(void)btnClick{ [self.navigationController popViewControllerAnimated:YES]; } #pragma mark UITableViewDelegate //将要开始输入 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ searchBar.text = nil; [_searchArray removeAllObjects]; return YES; } //正在输入时 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ for (NSDictionary* dic in _sourceData){ NSRange range = [dic[@"name"] rangeOfString:searchText]; if (range.location !=NSNotFound) { [_searchArray addObject:dic[@"name"]]; } [self.tableView reloadData]; } }// called when text changes (including clear) //搜索点击搜索按钮时 -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { for (NSDictionary* dic in _sourceData) { NSRange range = [dic[@"name"] rangeOfString:searchBar.text]; if (range.location !=NSNotFound) { [_searchArray addObject:dic[@"name"]]; } [self.tableView reloadData]; } } @end
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface UIView (FindChildView) - (UIView*)subViewOfClassName:(NSString*)className; @end NS_ASSUME_NONNULL_END
#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
原文:https://www.cnblogs.com/kingstudy/p/11509573.html