// // HomeSearchController.m // TheoryNetwork // // Created by tederen on 2019/9/20. // Copyright © 2019 tederen. All rights reserved. // #import "HomeSearchController.h" #import "InfoSearchView.h" #import "SearchTypeSection.h" #import "SearchTypeCell.h" #import "SearchHistoryCell.h" #import "HomeSearchDetailController.h" #import "SearchDetailViewCell.h" #import "HomeDetailController.h" #import "MyApprovalBCell.h" #import #import "MyApprovalPageDetail.h" #define PYSEARCH_MARGIN 10 static NSString * SearchDetailViewCellID = @"SearchDetailViewCellId"; static NSString * MyApprovalBCellID = @"MyApprovalBCell"; @interface HomeSearchController () @property (nonatomic, strong) InfoSearchView *topNavSearch; @property (nonatomic, strong) UIView *hisNoView; @property (nonatomic, copy) NSArray *warmArray; @property (nonatomic, copy) NSArray *coldArray; @property (nonatomic, assign) NSInteger coldPage; @property (nonatomic, assign) NSInteger warmPage; // 历史记录tableView @property (nonatomic, strong) TDTableView *historyTableView; // 历史记录tableViewHeaderContainer @property (nonatomic, strong) UIView *historyHeaderContainer; // 历史记录tableHeaderView @property (nonatomic, strong) UIView *historyTableHeaderView; @property (nonatomic, strong)SearchTypeSection *historyHeaderTitleView; // 联想tableView //@property (nonatomic, strong) TDTableView *thinkTableView; // 搜索结果tableView @property (nonatomic, strong) TDTableView *resultTableView; // 历史记录 @property (nonatomic, strong) NSMutableArray *historyArray; // 联想 @property (nonatomic, strong) NSMutableArray *thinkArray; // 搜索结果 @property (nonatomic, strong) NSMutableArray *resultArray; @property (nonatomic, copy) NSString *searchStr; @property (nonatomic, strong) UIView *noDataView; @property (nonatomic, strong) UILabel *noDataTipLabel; @end @implementation HomeSearchController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; //TODO:测试数据 _warmArray = @[@"信仰的力量", @"牛顿改革"]; _coldArray = @[@"科技", @"国家组织", @"泰德仁和"]; [self.view addSubview:self.topNavSearch]; [self.view addSubview:self.historyTableView]; [self initHistoryTableHeaderView]; // [self.view addSubview:self.thinkTableView]; [self.view addSubview:self.resultTableView]; [self.view addSubview:self.noDataView]; self.noDataView.frame = self.historyTableView.frame; [self.noDataView addSubview:self.noDataTipLabel]; // [self.view mas_makeConstraints:^(MASConstraintMaker *make) { // make.left.right.bottom.equalTo(self.view); // make.top.equalTo(self.topNavSearch.mas_bottom); // }]; [self.noDataTipLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.noDataView); make.top.mas_offset(32); }]; [self.noDataView setHidden:YES]; // [self getData]; self.coldPage = 1; self.warmPage = 1; [self.resultTableView registerClass:[SearchDetailViewCell class] forCellReuseIdentifier:SearchDetailViewCellID]; [self.resultTableView registerNib:[UINib nibWithNibName:@"MyApprovalBCell" bundle:nil] forCellReuseIdentifier:MyApprovalBCellID]; [self.topNavSearch.searchBar becomeFirstResponder]; [self.historyTableView setHidden:NO]; // [self.thinkTableView setHidden:YES]; // self.thinkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//推荐该方法 [self.resultTableView setHidden:YES]; } // 请求列表 - (void)getListData:(NSString *)keyword IsClickSearchAction:(BOOL)isClickSearchAction{ WEAKSELF if (isClickSearchAction) { SHOWLOADING [weakSelf.noDataView setHidden:YES]; [self.resultTableView setHidden:NO]; [self.historyTableView setHidden:YES]; // [self.thinkTableView setHidden:YES]; // [self.thinkTableView setHidden:YES]; } [self.thinkArray removeAllObjects]; [self.resultArray removeAllObjects]; // [self.sourceDataArr removeAllObjects]; [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Notice_list_Post] parameters: @{@"page":@1,@"perPage":@100,@"keyword":keyword} responseStyle:JOSN success:^(id _Nonnull responseObject) { REMOVESHOW NSDictionary *dic = (NSDictionary *)responseObject; NSLog(@"首页搜索列表数据%@=====================================",dic[@"total"]); HomeArticleModel *model = [HomeArticleModel yy_modelWithDictionary:dic]; weakSelf.thinkArray = [NSMutableArray arrayWithArray: model.Items]; weakSelf.resultArray = [NSMutableArray arrayWithArray: model.Items]; [weakSelf.noDataView setHidden:YES]; if (ISEmptyString(self.searchStr)) { //显示历史记录 [weakSelf.historyTableView setHidden:NO]; // [weakSelf.thinkTableView reloadData]; // [weakSelf.thinkTableView setHidden:YES]; [weakSelf.resultTableView reloadData]; [weakSelf.resultTableView setHidden:YES]; } else { // if (!isClickSearchAction) { // if (!model.Items.count) { // // 文章列表为空,显示历史记录 // // 文章列表不为空 // [weakSelf.historyTableView setHidden:NO]; //// [weakSelf.thinkTableView reloadData]; //// [weakSelf.thinkTableView setHidden:YES]; // [weakSelf.resultTableView reloadData]; // [weakSelf.resultTableView setHidden:YES]; // } else { // // 文章列表不为空 // [weakSelf.historyTableView setHidden:YES]; //// [weakSelf.thinkTableView reloadData]; //// [weakSelf.thinkTableView setHidden:NO]; // [weakSelf.resultTableView setHidden:YES]; // } // } else { // 点击搜索按钮 if (!model.Items.count) { // 文章列表为空,显示无结果 [self.noDataView setHidden:NO]; } else { // 文章列表不为空 [weakSelf.historyTableView setHidden:YES]; // [weakSelf.thinkTableView setHidden:YES]; [weakSelf.resultTableView reloadData]; [weakSelf.resultTableView setHidden:NO]; } // } } } failure:^(NSError * _Nonnull error) { SHOWERROR([ZYCTool handerResultData:error]) }]; } - (BOOL)hidesBottomBarWhenPushed { return YES; } //- (void)getData{ // [self getData1:1 page:1 perPage:3]; // [self getData2:2 page:1 perPage:3]; //} //- (void)getData1:(NSInteger )thesaurusType page:(NSInteger)page perPage:(NSInteger)perPage{ // // WEAKSELF // [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Search_Thesaurus_Post] parameters:@{@"thesaurusType":@(thesaurusType),@"page":@(page),@"perPage":@(perPage)} responseStyle:JOSN success:^(id _Nonnull responseObject) { // STRONGSELF // NSLog(@"热词%@",responseObject); // NSArray *array = responseObject; // NSMutableArray *arr = [NSMutableArray array]; // for (NSDictionary *dic in array) { // ThesaurusName *model = [[ThesaurusName alloc] initWithDictionary:dic error:nil]; // [arr addObject:model.ThesaurusName]; // } // strongSelf.warmArray = arr; // [strongSelf.historyTableView reloadSection:0 withRowAnimation:UITableViewRowAnimationAutomatic]; // } failure:^(NSError * _Nonnull error) { // // }]; //} //- (void)getData2:(NSInteger )thesaurusType page:(NSInteger)page perPage:(NSInteger)perPage{ // // WEAKSELF // [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Search_Thesaurus_Post] parameters:@{@"thesaurusType":@(thesaurusType),@"page":@(page),@"perPage":@(perPage)} responseStyle:JOSN success:^(id _Nonnull responseObject) { // STRONGSELF // NSLog(@"冷词%@",responseObject); // NSArray *array = responseObject; // NSMutableArray *arr = [NSMutableArray array]; // for (NSDictionary *dic in array) { // ThesaurusName *model = [[ThesaurusName alloc] initWithDictionary:dic error:nil]; // [arr addObject:model.ThesaurusName]; // } // strongSelf.coldArray = arr; // [strongSelf.historyTableView reloadSection:1 withRowAnimation:UITableViewRowAnimationAutomatic]; // } failure:^(NSError * _Nonnull error) { // // }]; //} - (void)viewWillAppear:(BOOL)animated { // [self.historyTableView reloadData]; [super viewWillAppear:animated]; [self.navigationController.navigationBar setHidden:YES]; } // 跳转到列表 //- (void)goDetailController:(NSString *)data { // [self insertHistorySearchStr:data]; // HomeSearchDetailController *searchDetailCon = [[HomeSearchDetailController alloc] init]; // [self.navigationController pushViewController:searchDetailCon animated:YES]; // [searchDetailCon setCurrentSearchData:data]; //} #pragma mark - InfoSearchViewDelegate // 点击搜索按钮 - (void)searchButtonAction:(UISearchBar *)searchBar { self.searchStr = searchBar.text; // 添加历史记录,刷新historyTable [self insertHistorySearchStr:self.searchStr]; // 更新本地历史记录 self.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]]; // 更新tableViewHeader的高度 if (self.historyArray.count == 0) { self.hisNoView.hidden = NO; }else{ self.hisNoView.hidden = YES; } [self addAndLayoutTagsWithTagsContentView:self.historyTableHeaderView tagTexts:self.historyArray]; [self resetHistoryHeaderViewHeight]; [self.historyTableView reloadData]; // 网络请求 [self getListData:self.searchStr IsClickSearchAction:YES]; [self.view endEditing:YES]; } // 文本改变回调 - (void)textDidChange:(NSString *)text { self.searchStr = text; if (ISEmptyString(text)) { // 没有文本显示历史记录cell [self.noDataView setHidden:YES]; [self.historyTableView setHidden:NO]; // [self.thinkTableView setHidden:YES]; [self.resultTableView setHidden:YES]; } else { // 有文本,就去搜索文章列表,有联想显示联想,没有联想显示历史记录 [self getListData:text IsClickSearchAction:NO]; } } #pragma mark - UITableViewDataSource && UITableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; // return 3; } //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { // if ([tableView isEqual:self.historyTableView]) { // return 50; // } else { // return 0; // } //} - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.1; } //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // if (![tableView isEqual:self.historyTableView]) { // return nil; // } // SearchTypeSection *searchSection = [[SearchTypeSection alloc] initWithFrame:CGRectMake(0, 16, kGXScreenWidth, 34.f)]; // switch (section) { // case 0: // { // [searchSection setSectionTitle:@"热词"]; // [searchSection setButtonTitle:@"换一批"]; // WEAKSELF // searchSection.rightAction = ^{ // STRONGSELF // if (section == 2 && section == 1) { // return ; // } // [strongSelf getData1:1 page:strongSelf.warmPage + 1 perPage:3]; //// strongSelf.warmArray = @[strongSelf.warmArray[1],strongSelf.warmArray[0]]; //// [tableView reloadSection:section withRowAnimation:UITableViewRowAnimationAutomatic]; // }; // // } // break; // case 1: // { // [searchSection setSectionTitle:@"推冷"]; // [searchSection setButtonTitle:@"换一批"]; // WEAKSELF // searchSection.rightAction = ^{ // STRONGSELF // if (section == 0 && section == 2) { // return ; // } // [strongSelf getData2:2 page:strongSelf.coldPage + 1 perPage:3]; //// strongSelf.coldArray = @[strongSelf.coldArray[2],strongSelf.coldArray[0],strongSelf.coldArray[1]]; //// [tableView reloadSection:section withRowAnimation:UITableViewRowAnimationAutomatic]; // }; // } // break; // case 2: // { // [searchSection setSectionTitle:@"最近搜索"]; // WEAKSELF // searchSection.rightAction = ^{ // // 清空历史记录 // STRONGSELF // if (section == 0 && section == 1) { // return ; // } // [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:@[]]; // strongSelf.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]]; // [tableView reloadData]; // }; // } // break; // default: // break; // } // return searchSection; //} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ([tableView isEqual:self.historyTableView]) { return 0; } // else if ([tableView isEqual:self.thinkTableView]) { // return self.thinkArray.count; // } else { return self.resultArray.count; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([tableView isEqual:self.historyTableView]) { return 34; } // else if ([tableView isEqual:self.thinkTableView]) { // return 44; // } else { Item *model = self.resultArray[indexPath.row]; if (model.SourceType == 0) { UILabel * label = [UILabel new]; label.numberOfLines = 2; label.text = model.Summary; // [label highlightString:model.Summary color:UIColorHex(EA3729) withSpace:7.f withFont:[UIFont systemFontOfSize:15.f] setLineSpace:0 setTextSpace:0]; CGFloat H = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT)].height; NSLog(@"height:%f",H); return 86 + H + (H > 36 ? 15 : 0); }else{ return 159.f; } } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([tableView isEqual:self.historyTableView]) { SearchHistoryCell * searchHistoryCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"SearchHistoryCell"]; if (!searchHistoryCell) { searchHistoryCell = [[SearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchHistoryCell"]; } [searchHistoryCell setCurrentHistoryData:_historyArray[indexPath.row]]; // WEAKSELF // searchHistoryCell.deleteAction = ^{ // // 删除单条历史记录 // STRONGSELF // [strongSelf.historyArray removeObjectAtIndex:indexPath.row]; // [_TDFileStorage saveMainSearchArray:strongSelf.historyArray]; // [tableView reloadData]; // }; // searchHistoryCell.searchAction = ^(NSString *searchStr) { // // 点击某个历史记录 // STRONGSELF // self.searchStr = searchStr; // [strongSelf.topNavSearch setNowSearchData:self.searchStr]; // [self getListData:searchStr IsClickSearchAction:YES]; // }; return searchHistoryCell; } // else if ([tableView isEqual:self.thinkTableView]) { // Item *tmpItem = self.thinkArray[indexPath.row]; // UITableViewCell * thinkCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"thinkCell"]; // if (!thinkCell) { // thinkCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"thinkCell"]; // thinkCell.textLabel.font = Kfont(14); // thinkCell.textLabel.textColor = kColorFromRGB(0x999999); // } // thinkCell.selectionStyle = UITableViewCellSelectionStyleNone; // thinkCell.textLabel.attributedText = [ZYCTool checkOfString:tmpItem.Title withSearchText:self.searchStr]; // return thinkCell; // } else { Item *model = self.resultArray[indexPath.row]; if (model.SourceType == 0) { SearchDetailViewCell *cell = (SearchDetailViewCell *)[tableView dequeueReusableCellWithIdentifier:SearchDetailViewCellID]; if (!cell) { cell = [[SearchDetailViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SearchDetailViewCellID]; } [cell loadCurrentData:model hotWord:self.searchStr]; return cell; }else{ MyApprovalBCell *cell = (MyApprovalBCell *)[tableView dequeueReusableCellWithIdentifier:MyApprovalBCellID]; [cell loadSearchData:model]; cell.falgLab.hidden = YES; return cell; } } // UITableViewCell *cell; // if (indexPath.section == 2) { // NSString *cellIdentifier = @"SearchHistoryCell"; // SearchHistoryCell * searchHistoryCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; // if (!searchHistoryCell) { // searchHistoryCell = [[SearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; // } // [searchHistoryCell setCurrentHistoryData:_historyArray[indexPath.row]]; // WEAKSELF // searchHistoryCell.deleteAction = ^{ // // 清空搜索历史记录 // STRONGSELF // [strongSelf.historyArray removeObjectAtIndex:indexPath.row]; // [_TDFileStorage saveMainSearchArray:strongSelf.historyArray]; // [tableView reloadData]; // }; // searchHistoryCell.searchAction = ^(NSString *searchStr) { // STRONGSELF // [strongSelf.topNavSearch setNowSearchData:searchStr]; // [strongSelf goDetailController:searchStr]; // }; // cell = searchHistoryCell; // } else { // NSString *cellIdentifier = @"SearchTypeCell"; // SearchTypeCell * searchTypeCell = (SearchTypeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; // if (!searchTypeCell) { // searchTypeCell = [[SearchTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; // } // if (!indexPath.section) { // [searchTypeCell setCurrentDataArray:_warmArray]; // } else { // [searchTypeCell setCurrentDataArray:_coldArray]; // } // WEAKSELF // searchTypeCell.seachAction = ^(NSString *searchStr){ // STRONGSELF // [strongSelf.topNavSearch setNowSearchData:searchStr]; // [strongSelf goDetailController:searchStr]; // }; // cell = searchTypeCell; // } // cell.selectionStyle = UITableViewCellSelectionStyleNone; // return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.view endEditing:YES]; if ([tableView isEqual:self.historyTableView]) { self.searchStr = self.historyArray[indexPath.row]; [self.topNavSearch setNowSearchData:self.searchStr]; [self getListData:self.searchStr IsClickSearchAction:YES]; // } // else if ([tableView isEqual:self.thinkTableView]) { // HomeDetailController *homeDetail = [[HomeDetailController alloc] init]; // [homeDetail loadCurrentModel:self.thinkArray[indexPath.row]]; // [self.navigationController pushViewController:homeDetail animated:YES]; } else { Item *model = self.resultArray[indexPath.row]; if (model.SourceType == 0) { HomeDetailController *homeDetail = [[HomeDetailController alloc] init]; [homeDetail loadCurrentModel:self.thinkArray[indexPath.row]]; [self.navigationController pushViewController:homeDetail animated:YES]; }else{ MyApprovalPageDetail * myApprovalPageDetail = [[MyApprovalPageDetail alloc]init]; myApprovalPageDetail.indexId = model.FlowId; myApprovalPageDetail.title = model.Title; myApprovalPageDetail.TodoId = model.Id; myApprovalPageDetail.userId = model.UserId; if ([model.Type isEqualToString:@"ApproverCheck"]) { myApprovalPageDetail.pageType = Type_ONEA; }else{ myApprovalPageDetail.pageType = Type_ONEC; } [self.navigationController pushViewController:myApprovalPageDetail animated:YES]; } } } #pragma mark - 查重 - (void)insertHistorySearchStr:(NSString *)string { for (NSString *cache in _historyArray) { // 查重 if ([cache isEqualToString:string]) { return; } } [_historyArray insertObject:string atIndex:0]; if (_historyArray.count > 10) { NSArray *orderArray = [_historyArray subarrayWithRange:NSMakeRange(0, 10)]; _historyArray = orderArray.mutableCopy; } [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:_historyArray]; } #pragma mark - getter - (InfoSearchView *)topNavSearch { if (!_topNavSearch) { _topNavSearch = [[InfoSearchView alloc] initWithFrame:CGRectMake(0, (IS_IPHONEX?40:20), kGXScreenWidth, 45) type:InfoSearchViewTypeDefault]; _topNavSearch.delegate = self; WEAKSELF _topNavSearch.backBlock = ^{ STRONGSELF [strongSelf.navigationController popViewControllerAnimated:YES]; }; [_topNavSearch showLineView]; } return _topNavSearch; } - (TDTableView *)historyTableView { if (!_historyTableView) { _historyTableView = [[TDTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _historyTableView.dataSource = self; _historyTableView.delegate = self; _historyTableView.frame = CGRectMake(0, (IS_IPHONEX?40:20)+45, kGXScreenWidth, kGXScreenHeigh-(IS_IPHONEX?40:20)-45); } return _historyTableView; } //- (TDTableView *)thinkTableView { // if (!_thinkTableView) { // _thinkTableView = [[TDTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; // _thinkTableView.dataSource = self; // _thinkTableView.delegate = self; // _thinkTableView.frame = CGRectMake(0, (IS_IPHONEX?40:20)+45, kGXScreenWidth, kGXScreenHeigh-(IS_IPHONEX?40:20)-45); // [_thinkTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; // } // return _thinkTableView; //} - (TDTableView *)resultTableView { if (!_resultTableView) { _resultTableView = [[TDTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _resultTableView.dataSource = self; _resultTableView.delegate = self; _resultTableView.frame = CGRectMake(0, (IS_IPHONEX?40:20)+45, kGXScreenWidth, kGXScreenHeigh-(IS_IPHONEX?40:20)-45); } return _resultTableView; } - (NSMutableArray *)historyArray { if (!_historyArray) { _historyArray = [_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home].mutableCopy; } return _historyArray; } - (NSMutableArray *)thinkArray { if (!_thinkArray) { _thinkArray = [NSMutableArray array]; } return _thinkArray; } - (NSMutableArray *)resultArray { if (!_resultArray) { _resultArray = [NSMutableArray array]; } return _resultArray; } - (UIView *)noDataView { if (!_noDataView) { _noDataView = [UIView new]; _noDataView.backgroundColor = [UIColor whiteColor]; } return _noDataView; } - (UILabel *)noDataTipLabel { if (!_noDataTipLabel) { _noDataTipLabel = [UILabel new]; _noDataTipLabel.text = @"暂无搜索结果"; _noDataTipLabel.font = Kfont(15); _noDataTipLabel.textColor = kColorFromRGB(0x999999); _noDataTipLabel.textAlignment = NSTextAlignmentCenter; } return _noDataTipLabel; } - (UIView *)historyTableHeaderView { if (!_historyTableHeaderView) { _historyTableHeaderView = [UIView new]; _historyTableHeaderView.backgroundColor = [UIColor whiteColor];; } return _historyTableHeaderView; } - (SearchTypeSection *)historyHeaderTitleView { if (!_historyHeaderTitleView) { _historyHeaderTitleView = [[SearchTypeSection alloc] initWithFrame:CGRectMake(0, 0, kGXScreenWidth, 44)]; } return _historyHeaderTitleView; } #pragma mark - historyHeaderView // 添加tagView - (void)addAndLayoutTagsWithTagsContentView:(UIView *)contentView tagTexts:(NSArray *)tagTexts; { [contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; NSMutableArray *tagsM = [NSMutableArray array]; for (int i = 0; i < tagTexts.count; i++) { UILabel *label = [self labelWithTitle:tagTexts[i]]; [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagDidCLick:)]]; [contentView addSubview:label]; [tagsM addObject:label]; } CGFloat currentX = 0; CGFloat currentY = 0; CGFloat countRow = 0; CGFloat countCol = 0; CGFloat leftMargin = 12; for (int i = 0; i < contentView.subviews.count; i++) { UILabel *subView = contentView.subviews[i]; // When the number of search words is too large, the width is width of the contentView if (subView.width > contentView.width) { subView.width = contentView.width; } if (leftMargin + currentX + subView.width + PYSEARCH_MARGIN * countRow > contentView.width) { subView.x = leftMargin; subView.y = (currentY += subView.height) + PYSEARCH_MARGIN * ++countCol; currentX = subView.width; countRow = 1; } else { subView.x = leftMargin + (currentX += subView.width) - subView.width + PYSEARCH_MARGIN * countRow; subView.y = currentY + PYSEARCH_MARGIN * countCol; countRow ++; } } } // 创建label - (UILabel *)labelWithTitle:(NSString *)title { UILabel *label = [[UILabel alloc] init]; label.userInteractionEnabled = YES; label.font = [UIFont systemFontOfSize:14]; label.text = title; label.textColor = [UIColor grayColor]; label.backgroundColor = kColorFromRGB(0xfafafa); label.textAlignment = NSTextAlignmentCenter; [label sizeToFit]; label.width += 20; label.height += 14; label.layer.cornerRadius = label.height / 2; label.clipsToBounds = YES; return label; } - (void)tagDidCLick:(UITapGestureRecognizer *)gr { UILabel *label = (UILabel *)gr.view; self.searchStr = label.text; [self.topNavSearch setNowSearchData:self.searchStr]; [self getListData:self.searchStr IsClickSearchAction:YES]; } - (UIView *)historyHeaderContainer { if (!_historyHeaderContainer) { _historyHeaderContainer = [UIView new]; _historyHeaderContainer.backgroundColor = [UIColor whiteColor]; } return _historyHeaderContainer; } // 创建historyTableView的headerView - (void)initHistoryTableHeaderView { [self.historyHeaderTitleView setSectionTitle:@"最近搜索"]; WEAKSELF self.historyHeaderTitleView.rightAction = ^{ UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 清空历史记录 [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:@[]]; weakSelf.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]]; [weakSelf addAndLayoutTagsWithTagsContentView:weakSelf.historyTableHeaderView tagTexts:weakSelf.historyArray]; [weakSelf resetHistoryHeaderViewHeight]; if (weakSelf.historyArray.count == 0) { weakSelf.hisNoView.hidden = NO; }else{ weakSelf.hisNoView.hidden = YES; } [weakSelf.historyTableView reloadData]; }]; UIAlertController *tmpVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除最近搜索?" preferredStyle:UIAlertControllerStyleAlert]; [tmpVc addAction:action1]; [tmpVc addAction:action2]; [weakSelf presentViewController:tmpVc animated:YES completion:nil]; }; [self.historyHeaderContainer addSubview:self.historyHeaderTitleView]; self.historyTableHeaderView.frame = CGRectMake(0, self.historyHeaderTitleView.height + 13, kScreenWidth, 0); [self addAndLayoutTagsWithTagsContentView:self.historyTableHeaderView tagTexts:self.historyArray]; CGFloat height = CGRectGetMaxY(self.historyTableHeaderView.subviews.lastObject.frame); self.historyTableHeaderView.frame = CGRectMake(0, self.historyHeaderTitleView.height + 13, kScreenWidth, height); [self.historyHeaderContainer addSubview:self.historyTableHeaderView]; [self resetHistoryHeaderViewHeight]; self.historyTableView.tableHeaderView = self.historyHeaderContainer; self.historyTableView.tableFooterView = self.hisNoView; if (self.historyArray.count == 0) { self.hisNoView.hidden = NO; }else{ self.hisNoView.hidden = YES; } UILabel * label = [UILabel new]; label.text = @"暂无搜索记录!"; label.font = [UIFont systemFontOfSize:14]; label.textColor = UIColorHex(0x999999); [self.hisNoView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.center.mas_equalTo(self.hisNoView); }]; } - (UIView *)hisNoView { if (!_hisNoView) { _hisNoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)]; _hisNoView.hidden = YES; } return _hisNoView; } // 更新header高度 - (void)resetHistoryHeaderViewHeight { CGFloat containerHeight = CGRectGetMaxY(self.historyHeaderContainer.subviews.lastObject.frame); self.historyHeaderContainer.frame = CGRectMake(0, 0, kScreenWidth, containerHeight); } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self.topNavSearch.searchBar resignFirstResponder]; } @end