// // KDPSearchViewController.m // KuDianProject // // Created by admin on 2019/7/10. // Copyright © 2019 KDP. All rights reserved. // #import "KDPSearchViewController.h" #import "MSSAutoresizeLabelFlow.h" #import "KDPSearchResultViewController.h" @interface KDPSearchViewController () @property (nonatomic, strong) UITableView *tableview; @property (nonatomic, strong) NSMutableArray *historyDataArray; @property (nonatomic, strong) NSMutableArray *searchArray; @property (nonatomic, strong) UIView *historyView; @property (nonatomic, strong) UITextField *searchTextField; @property (nonatomic, strong) MSSAutoresizeLabelFlow *flowView; @end @implementation KDPSearchViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setUpNav]; [self loadHisttoryData]; [self setContentView]; } - (void)setUpNav{ [self.navBar setLineViewWithHidden:YES]; self.navBar.backgroundColor = [UIColor colorWithHex:0xf9f9f9]; self.view.backgroundColor = [UIColor whiteColor]; UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(10, KDStatusHeight + 7, SCREEN_WIDTH-85, 31)]; searchView.layer.cornerRadius = 16; searchView.layer.masksToBounds = YES; searchView.backgroundColor = [UIColor whiteColor]; [self.navBar addSubview:searchView]; UIImageView *searchIcon = [[UIImageView alloc] initWithFrame:CGRectMake(13, 8, 15, 15)]; searchIcon.image = [UIImage imageNamed:@"search_icon"]; [searchView addSubview:searchIcon]; self.searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(searchIcon.right + 7, 4.5, searchView.width-45, 22)]; self.searchTextField.delegate = self; self.searchTextField.font = FONT_SYS(14); self.searchTextField.textColor = [UIColor colorWithHex:0x8F8F95]; self.searchTextField.returnKeyType = UIReturnKeySearch; self.searchTextField.backgroundColor = [UIColor clearColor]; self.searchTextField.placeholder = @"搜索想要的内容"; [searchView addSubview:self.searchTextField]; UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; cancelBtn.frame = CGRectMake(SCREEN_WIDTH-55, 0, 35, 21); cancelBtn.centerY = searchView.centerY; [cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; [cancelBtn setTitleColor:[UIColor colorWithHex:0x666666] forState:UIControlStateNormal]; cancelBtn.titleLabel.font = FONT_SYS(15); [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside]; [self.navBar addSubview:cancelBtn]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textchangeclick) name:UITextFieldTextDidChangeNotification object:nil]; } - (void)loadHisttoryData{ [self.historyDataArray removeAllObjects]; NSArray *saveHistory = [NSKeyedUnarchiver unarchiveObjectWithFile:SearchPath]; [self.historyDataArray addObjectsFromArray:saveHistory]; } - (void)setContentView{ [self.view addSubview:self.tableview]; [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(UIEdgeInsetsMake(KDNavBarHeight, 0, 0, 0)); }]; [self.view addSubview:self.historyView]; [self.historyView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(UIEdgeInsetsZero); }]; UILabel *searchHistLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, KDNavBarHeight + 10, 100, 30)]; searchHistLabel.text = @"搜索历史"; searchHistLabel.textColor = [UIColor colorWithHex:0x666666]; searchHistLabel.font = FONT_SYS(14); [self.historyView addSubview:searchHistLabel]; UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [deleteBtn setTitle:@"清空" forState:UIControlStateNormal]; [deleteBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal]; [deleteBtn setImage:[UIImage imageNamed:@"delete_collection"] forState:UIControlStateNormal]; [deleteBtn addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside]; deleteBtn.titleLabel.font = FONT_SYS(13); [deleteBtn setButtonStyle:WSLButtonStyleImageLeft spacing:3]; [self.historyView addSubview:deleteBtn]; [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(KDNavBarHeight + 10); make.right.equalTo(-20); make.height.equalTo(30); }]; self.flowView = [[MSSAutoresizeLabelFlow alloc] initWithFrame:CGRectMake(0, searchHistLabel.bottom, self.view.width, 10) titles:self.historyDataArray selectedHandler:^(NSUInteger index, NSString *title) { [self skipToResultWithString:title]; }]; [self.historyView addSubview:self.flowView]; self.historyView.hidden = self.historyDataArray.count == 0 ? YES : NO; } - (void)requestSearchData:(NSString *)keyWord{ NSString *searchUrl = [NSString stringWithFormat:@"%@api/goods/searchAdvice",KDURL]; [KDPNetworkRequestHTTP postURL:searchUrl params:@{@"keyword":keyWord} success:^(id _Nonnull json) { NSArray *data = json[@"data"]; [self.searchArray removeAllObjects]; [self.searchArray addObjectsFromArray:data]; if (self.searchArray.count) { self.historyView.hidden = YES; self.tableview.hidden = NO; } else{ self.historyView.hidden = NO; self.tableview.hidden = YES; } [self.tableview reloadData]; } failure:^(NSError * _Nonnull error) { }]; } - (void)skipToResultWithString:(NSString *)searchText{ if (![self.historyDataArray containsObject:searchText]) { [self.historyDataArray addObject:searchText]; } [self saveHistory]; // 跳转到结果页 KDPSearchResultViewController *searchResultVC = [[KDPSearchResultViewController alloc] init]; searchResultVC.keyWordStr = searchText; [self.navigationController pushViewController:searchResultVC animated:NO]; } - (void)saveHistory{ BOOL success = [NSKeyedArchiver archiveRootObject:self.historyDataArray toFile:SearchPath]; if (success) { NSLog(@"保存成功"); [self.flowView reloadAllWithTitles:self.historyDataArray]; } } - (void)cancelAction:(UIButton *)sender{ [self.navigationController popViewControllerAnimated:NO]; } - (void)textchangeclick{ if (self.searchTextField.text.length>0) { [self requestSearchData:self.searchTextField.text]; } } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { if ( textField.text.length>0) { [self skipToResultWithString:textField.text]; } return YES; } - (void)deleteAction{ [self.historyDataArray removeAllObjects]; [self saveHistory]; [self.flowView reloadAllWithTitles:self.historyDataArray]; self.historyView.hidden = YES; } #pragma UITableview delegate and datasource - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchList"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchList"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)]; lineView.backgroundColor = [UIColor colorWithHex:0xf4f4f4]; [cell.contentView addSubview:lineView]; } cell.textLabel.text = self.searchArray[indexPath.row]; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.searchArray.count; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self skipToResultWithString:self.searchArray[indexPath.row]]; } #pragma mark - 懒加载 - (NSMutableArray *)historyDataArray{ if (!_historyDataArray) { _historyDataArray = [NSMutableArray array]; } return _historyDataArray; } - (NSMutableArray *)searchArray{ if (!_searchArray) { _searchArray = [NSMutableArray array]; } return _searchArray; } - (UITableView *)tableview{ if (!_tableview) { _tableview = [[UITableView alloc] init]; _tableview.delegate = self; _tableview.dataSource = self; _tableview.backgroundColor = [UIColor whiteColor]; _tableview.separatorStyle = UITableViewCellSeparatorStyleNone; _tableview.hidden = YES; _tableview.showsVerticalScrollIndicator = NO; _tableview.showsHorizontalScrollIndicator = NO; _tableview.rowHeight = 50; [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"searchList"]; } return _tableview; } - (UIView *)historyView{ if (!_historyView) { _historyView = [[UIView alloc] init]; _historyView.backgroundColor = [UIColor whiteColor]; } return _historyView; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ [self.view endEditing:YES]; } @end