diff --git a/PYSearch/PYSearchConst.h b/PYSearch/PYSearchConst.h index 04c691f..ec4c45c 100644 --- a/PYSearch/PYSearchConst.h +++ b/PYSearch/PYSearchConst.h @@ -11,7 +11,6 @@ #define PYMargin 10 // 默认边距 #define PYBackgroundColor PYColor(255, 255, 255) // tableView背景颜色 - // 日志输出 #ifdef DEBUG #define PYSearchLog(...) NSLog(__VA_ARGS__) diff --git a/PYSearch/PYSearchViewController.m b/PYSearch/PYSearchViewController.m index a25e361..746783b 100644 --- a/PYSearch/PYSearchViewController.m +++ b/PYSearch/PYSearchViewController.m @@ -89,7 +89,7 @@ - (UITableView *)baseSearchTableView { if (!_baseSearchTableView) { UITableView *baseSearchTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; - baseSearchTableView.backgroundColor = PYBackgroundColor; + baseSearchTableView.backgroundColor = [UIColor clearColor]; baseSearchTableView.delegate = self; baseSearchTableView.dataSource = self; [self.view addSubview:baseSearchTableView]; @@ -208,30 +208,6 @@ - (void)setCancelButton:(UIBarButtonItem *)cancelButton self.navigationItem.rightBarButtonItem = cancelButton; } -/** 视图加载完毕 */ -- (void)viewDidLoad { - [super viewDidLoad]; -} - -/** 视图将要显示 */ -- (void)viewWillAppear:(BOOL)animated -{ - [super viewWillAppear:animated]; - - // 没有热门搜索并且搜索历史为默认PYHotSearchStyleDefault就隐藏 - if (self.hotSearches.count == 0 && self.searchHistoryStyle == PYHotSearchStyleDefault) { - self.baseSearchTableView.tableHeaderView.py_height = 0; - self.baseSearchTableView.tableHeaderView.hidden = YES; - } - - // 刷新热门搜索 - [self setHotSearches:self.hotSearches]; - // 刷新热门搜索Style - [self setHotSearchStyle:self.hotSearchStyle]; - // 刷新搜索历史Style - [self setSearchHistoryStyle:self.searchHistoryStyle]; -} - /** 视图完全显示 */ - (void)viewDidAppear:(BOOL)animated { @@ -250,6 +226,8 @@ - (void)dealloc /** 初始化 */ - (void)setup { + // 设置背景颜色为白色 + self.view.backgroundColor = [UIColor whiteColor]; self.baseSearchTableView.separatorStyle = UITableViewCellSeparatorStyleNone; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancelDidClick)]; @@ -385,6 +363,8 @@ - (void)setupHotSearchRectangleTags verticalLine.py_width = contentView.py_width; [contentView addSubview:verticalLine]; } + // 重新赋值,注意:当操作系统为iOS 9.x系列的tableHeaderView高度设置失效,需要重新设置tableHeaderView + [self.baseSearchTableView setTableHeaderView:self.baseSearchTableView.tableHeaderView]; } /** 设置热门搜索标签(带有排名)PYHotSearchStyleRankTag */ @@ -487,6 +467,8 @@ - (void)setupHotSearchNormalTags { // 添加和布局标签 self.hotSearchTags = [self addAndLayoutTagsWithTagsContentView:self.hotSearchTagsContentView tagTexts:self.hotSearches]; + // 根据hotSearchStyle设置标签样式 + [self setHotSearchStyle:self.hotSearchStyle]; } /** @@ -545,6 +527,8 @@ - (NSArray *)addAndLayoutTagsWithTagsContentView:(UIView *)contentView tagTexts: contentView.py_height = CGRectGetMaxY(contentView.subviews.lastObject.frame); // 设置头部高度 self.baseSearchTableView.tableHeaderView.py_height = self.headerContentView.py_height = CGRectGetMaxY(contentView.frame) + PYMargin * 2; + // 取消隐藏 + self.baseSearchTableView.tableHeaderView.hidden = NO; // 重新赋值, 注意:当操作系统为iOS 9.x系列的tableHeaderView高度设置失效,需要重新设置tableHeaderView [self.baseSearchTableView setTableHeaderView:self.baseSearchTableView.tableHeaderView]; return [tagsM copy]; @@ -604,10 +588,12 @@ - (void)setHotSearches:(NSArray *)hotSearches // 没有热门搜索,隐藏相关控件,直接返回 if (hotSearches.count == 0) { self.baseSearchTableView.tableHeaderView.hidden = YES; + self.hotSearchHeader.hidden = YES; return; }; // 有热门搜索,取消相关隐藏 self.baseSearchTableView.tableHeaderView.hidden = NO; + self.hotSearchHeader.hidden = NO; // 根据hotSearchStyle设置标签 if (self.hotSearchStyle == PYHotSearchStyleDefault || self.hotSearchStyle == PYHotSearchStyleColorfulTag @@ -619,6 +605,8 @@ - (void)setHotSearches:(NSArray *)hotSearches } else if (self.hotSearchStyle == PYHotSearchStyleRectangleTag) { // 矩阵标签 [self setupHotSearchRectangleTags]; } + // 刷新搜索历史布局 + [self setSearchHistoryStyle:self.searchHistoryStyle]; } - (void)setSearchHistoryStyle:(PYSearchHistoryStyle)searchHistoryStyle @@ -836,7 +824,6 @@ - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar default: break; } - // 如果代理实现了代理方法则调用代理方法 if ([self.delegate respondsToSelector:@selector(searchViewController:didSearchWithsearchBar:searchText:)]) { [self.delegate searchViewController:self didSearchWithsearchBar:searchBar searchText:searchBar.text]; @@ -848,6 +835,8 @@ - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { + // 如果有搜索文本且显示搜索建议,则隐藏 + self.baseSearchTableView.hidden = searchText.length && !self.searchSuggestionHidden; // 根据输入文本显示建议搜索条件 self.searchSuggestionVC.view.hidden = self.searchSuggestionHidden || !searchText.length; // 放在最上层