HomeSearchController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. //
  2. // HomeSearchController.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/20.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "HomeSearchController.h"
  9. #import "InfoSearchView.h"
  10. #import "SearchTypeSection.h"
  11. #import "SearchTypeCell.h"
  12. #import "SearchHistoryCell.h"
  13. #import "SearchDetailViewCell.h"
  14. #import "HomeDetailController.h"
  15. #import "MyApprovalBCell.h"
  16. #import <YYModel/YYModel.h>
  17. #import "MyApprovalPageDetail.h"
  18. #import "DrawerView.h"
  19. #import "TabBarController.h"
  20. #define PYSEARCH_MARGIN 10
  21. static NSString * SearchDetailViewCellID = @"SearchDetailViewCellId";
  22. static NSString * MyApprovalBCellID = @"MyApprovalBCell";
  23. @interface HomeSearchController () <InfoSearchViewDelegate, UITableViewDataSource, UITableViewDelegate>
  24. @property (nonatomic, strong) InfoSearchView *topNavSearch;
  25. @property (nonatomic, strong) UIView *hisNoView;
  26. @property (strong,nonatomic) DrawerView *drawerView;
  27. @property (nonatomic, copy) NSArray *warmArray;
  28. @property (nonatomic, copy) NSArray *coldArray;
  29. @property (nonatomic, assign) NSInteger coldPage;
  30. @property (nonatomic, assign) NSInteger warmPage;
  31. // 历史记录tableView
  32. @property (nonatomic, strong) TDTableView *historyTableView;
  33. // 历史记录tableViewHeaderContainer
  34. @property (nonatomic, strong) UIView *historyHeaderContainer;
  35. // 历史记录tableHeaderView
  36. @property (nonatomic, strong) UIView *historyTableHeaderView;
  37. @property (nonatomic, strong)SearchTypeSection *historyHeaderTitleView;
  38. // 联想tableView
  39. //@property (nonatomic, strong) TDTableView *thinkTableView;
  40. // 搜索结果tableView
  41. @property (nonatomic, strong) UITableView *resultTableView;
  42. // 历史记录
  43. @property (nonatomic, strong) NSMutableArray *historyArray;
  44. // 联想
  45. @property (nonatomic, strong) NSMutableArray<Item *> *thinkArray;
  46. // 搜索结果
  47. @property (nonatomic, strong) NSMutableArray<Item *> *resultArray;
  48. @property (nonatomic, copy) NSString *searchStr;
  49. @property (nonatomic, strong) UIView *noDataView;
  50. @property (nonatomic, strong) UILabel *noDataTipLabel;
  51. @end
  52. @implementation HomeSearchController
  53. -(DrawerView *)drawerView{
  54. if(!_drawerView){
  55. _drawerView = [[DrawerView alloc] init];
  56. }
  57. return _drawerView;
  58. }
  59. - (void)viewDidLoad {
  60. [super viewDidLoad];
  61. self.view.backgroundColor = [UIColor whiteColor];
  62. //TODO:测试数据
  63. _warmArray = @[@"信仰的力量", @"牛顿改革"];
  64. _coldArray = @[@"科技", @"国家组织", @"泰德仁和"];
  65. [self.view addSubview:self.topNavSearch];
  66. [self.view addSubview:self.historyTableView];
  67. [self initHistoryTableHeaderView];
  68. // [self.view addSubview:self.thinkTableView];
  69. [self.view addSubview:self.resultTableView];
  70. [self.resultTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.left.right.mas_equalTo(self.view);
  72. make.top.mas_equalTo(self.topNavSearch.mas_bottom);
  73. if (@available(iOS 11.0, *)) {
  74. make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
  75. } else {
  76. make.bottom.equalTo(self.view.mas_bottom);
  77. }
  78. }];
  79. [self.view addSubview:self.noDataView];
  80. self.noDataView.frame = self.historyTableView.frame;
  81. [self.noDataView addSubview:self.noDataTipLabel];
  82. // [self.view mas_makeConstraints:^(MASConstraintMaker *make) {
  83. // make.left.right.bottom.equalTo(self.view);
  84. // make.top.equalTo(self.topNavSearch.mas_bottom);
  85. // }];
  86. [self.noDataTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.left.right.equalTo(self.noDataView);
  88. make.top.mas_offset(32);
  89. }];
  90. [self.noDataView setHidden:YES];
  91. // [self getData];
  92. self.coldPage = 1;
  93. self.warmPage = 1;
  94. [self.resultTableView registerNib:[UINib nibWithNibName:@"MyApprovalBCell" bundle:nil] forCellReuseIdentifier:MyApprovalBCellID];
  95. [self.topNavSearch.searchBar becomeFirstResponder];
  96. [self.historyTableView setHidden:NO];
  97. // [self.thinkTableView setHidden:YES];
  98. // self.thinkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//推荐该方法
  99. [self.resultTableView setHidden:YES];
  100. }
  101. // 请求列表
  102. - (void)getListData:(NSString *)keyword IsClickSearchAction:(BOOL)isClickSearchAction{
  103. WEAKSELF
  104. if (isClickSearchAction) {
  105. SHOWLOADING
  106. [weakSelf.noDataView setHidden:YES];
  107. [self.resultTableView setHidden:NO];
  108. [self.historyTableView setHidden:YES];
  109. // [self.thinkTableView setHidden:YES];
  110. // [self.thinkTableView setHidden:YES];
  111. }
  112. [self.thinkArray removeAllObjects];
  113. [self.resultArray removeAllObjects];
  114. // [self.sourceDataArr removeAllObjects];
  115. [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Notice_list_Post] parameters: @{@"page":@1,@"perPage":@100,@"keyword":keyword} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  116. REMOVESHOW
  117. NSDictionary *dic = (NSDictionary *)responseObject;
  118. NSLog(@"首页搜索列表数据%@=====================================",dic[@"total"]);
  119. HomeArticleModel *model = [HomeArticleModel yy_modelWithDictionary:dic];
  120. weakSelf.thinkArray = [NSMutableArray arrayWithArray: model.Items];
  121. weakSelf.resultArray = [NSMutableArray arrayWithArray: model.Items];
  122. [weakSelf.noDataView setHidden:YES];
  123. if (ISEmptyString(self.searchStr)) {
  124. //显示历史记录
  125. [weakSelf.historyTableView setHidden:NO];
  126. // [weakSelf.thinkTableView reloadData];
  127. // [weakSelf.thinkTableView setHidden:YES];
  128. [weakSelf.resultTableView reloadData];
  129. [weakSelf.resultTableView setHidden:YES];
  130. } else {
  131. // if (!isClickSearchAction) {
  132. // if (!model.Items.count) {
  133. // // 文章列表为空,显示历史记录
  134. // // 文章列表不为空
  135. // [weakSelf.historyTableView setHidden:NO];
  136. //// [weakSelf.thinkTableView reloadData];
  137. //// [weakSelf.thinkTableView setHidden:YES];
  138. // [weakSelf.resultTableView reloadData];
  139. // [weakSelf.resultTableView setHidden:YES];
  140. // } else {
  141. // // 文章列表不为空
  142. // [weakSelf.historyTableView setHidden:YES];
  143. //// [weakSelf.thinkTableView reloadData];
  144. //// [weakSelf.thinkTableView setHidden:NO];
  145. // [weakSelf.resultTableView setHidden:YES];
  146. // }
  147. // } else {
  148. // 点击搜索按钮
  149. if (!model.Items.count) {
  150. // 文章列表为空,显示无结果
  151. [self.noDataView setHidden:NO];
  152. } else {
  153. // 文章列表不为空
  154. [weakSelf.historyTableView setHidden:YES];
  155. // [weakSelf.thinkTableView setHidden:YES];
  156. [weakSelf.resultTableView reloadData];
  157. [weakSelf.resultTableView setHidden:NO];
  158. }
  159. // }
  160. }
  161. } failure:^(NSError * _Nonnull error) {
  162. SHOWERROR([ZYCTool handerResultData:error])
  163. }];
  164. }
  165. - (BOOL)hidesBottomBarWhenPushed
  166. {
  167. return YES;
  168. }
  169. //- (void)getData{
  170. // [self getData1:1 page:1 perPage:3];
  171. // [self getData2:2 page:1 perPage:3];
  172. //}
  173. //- (void)getData1:(NSInteger )thesaurusType page:(NSInteger)page perPage:(NSInteger)perPage{
  174. //
  175. // WEAKSELF
  176. // [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Search_Thesaurus_Post] parameters:@{@"thesaurusType":@(thesaurusType),@"page":@(page),@"perPage":@(perPage)} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  177. // STRONGSELF
  178. // NSLog(@"热词%@",responseObject);
  179. // NSArray *array = responseObject;
  180. // NSMutableArray *arr = [NSMutableArray array];
  181. // for (NSDictionary *dic in array) {
  182. // ThesaurusName *model = [[ThesaurusName alloc] initWithDictionary:dic error:nil];
  183. // [arr addObject:model.ThesaurusName];
  184. // }
  185. // strongSelf.warmArray = arr;
  186. // [strongSelf.historyTableView reloadSection:0 withRowAnimation:UITableViewRowAnimationAutomatic];
  187. // } failure:^(NSError * _Nonnull error) {
  188. //
  189. // }];
  190. //}
  191. //- (void)getData2:(NSInteger )thesaurusType page:(NSInteger)page perPage:(NSInteger)perPage{
  192. //
  193. // WEAKSELF
  194. // [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Search_Thesaurus_Post] parameters:@{@"thesaurusType":@(thesaurusType),@"page":@(page),@"perPage":@(perPage)} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  195. // STRONGSELF
  196. // NSLog(@"冷词%@",responseObject);
  197. // NSArray *array = responseObject;
  198. // NSMutableArray *arr = [NSMutableArray array];
  199. // for (NSDictionary *dic in array) {
  200. // ThesaurusName *model = [[ThesaurusName alloc] initWithDictionary:dic error:nil];
  201. // [arr addObject:model.ThesaurusName];
  202. // }
  203. // strongSelf.coldArray = arr;
  204. // [strongSelf.historyTableView reloadSection:1 withRowAnimation:UITableViewRowAnimationAutomatic];
  205. // } failure:^(NSError * _Nonnull error) {
  206. //
  207. // }];
  208. //}
  209. - (void)viewWillAppear:(BOOL)animated {
  210. // [self.historyTableView reloadData];
  211. [super viewWillAppear:animated];
  212. [self.navigationController.navigationBar setHidden:YES];
  213. }
  214. // 跳转到列表
  215. //- (void)goDetailController:(NSString *)data {
  216. // [self insertHistorySearchStr:data];
  217. // HomeSearchDetailController *searchDetailCon = [[HomeSearchDetailController alloc] init];
  218. // [self.navigationController pushViewController:searchDetailCon animated:YES];
  219. // [searchDetailCon setCurrentSearchData:data];
  220. //}
  221. #pragma mark - InfoSearchViewDelegate
  222. // 点击搜索按钮
  223. - (void)searchButtonAction:(UISearchBar *)searchBar {
  224. self.searchStr = searchBar.text;
  225. // 添加历史记录,刷新historyTable
  226. [self insertHistorySearchStr:self.searchStr];
  227. // 更新本地历史记录
  228. self.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]];
  229. // 更新tableViewHeader的高度
  230. if (self.historyArray.count == 0) {
  231. self.hisNoView.hidden = NO;
  232. }else{
  233. self.hisNoView.hidden = YES;
  234. }
  235. [self addAndLayoutTagsWithTagsContentView:self.historyTableHeaderView tagTexts:self.historyArray];
  236. [self resetHistoryHeaderViewHeight];
  237. [self.historyTableView reloadData];
  238. // 网络请求
  239. [self getListData:self.searchStr IsClickSearchAction:YES];
  240. [self.view endEditing:YES];
  241. }
  242. // 文本改变回调
  243. - (void)textDidChange:(NSString *)text {
  244. self.searchStr = text;
  245. if (ISEmptyString(text)) {
  246. // 没有文本显示历史记录cell
  247. [self.noDataView setHidden:YES];
  248. [self.historyTableView setHidden:NO];
  249. // [self.thinkTableView setHidden:YES];
  250. [self.resultTableView setHidden:YES];
  251. } else {
  252. // 有文本,就去搜索文章列表,有联想显示联想,没有联想显示历史记录
  253. [self getListData:text IsClickSearchAction:NO];
  254. }
  255. }
  256. #pragma mark - UITableViewDataSource && UITableViewDelegate
  257. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  258. return 1;
  259. // return 3;
  260. }
  261. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  262. // if ([tableView isEqual:self.historyTableView]) {
  263. // return 50;
  264. // } else {
  265. // return 0;
  266. // }
  267. //}
  268. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  269. return 0.1;
  270. }
  271. //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  272. // if (![tableView isEqual:self.historyTableView]) {
  273. // return nil;
  274. // }
  275. // SearchTypeSection *searchSection = [[SearchTypeSection alloc] initWithFrame:CGRectMake(0, 16, kGXScreenWidth, 34.f)];
  276. // switch (section) {
  277. // case 0:
  278. // {
  279. // [searchSection setSectionTitle:@"热词"];
  280. // [searchSection setButtonTitle:@"换一批"];
  281. // WEAKSELF
  282. // searchSection.rightAction = ^{
  283. // STRONGSELF
  284. // if (section == 2 && section == 1) {
  285. // return ;
  286. // }
  287. // [strongSelf getData1:1 page:strongSelf.warmPage + 1 perPage:3];
  288. //// strongSelf.warmArray = @[strongSelf.warmArray[1],strongSelf.warmArray[0]];
  289. //// [tableView reloadSection:section withRowAnimation:UITableViewRowAnimationAutomatic];
  290. // };
  291. //
  292. // }
  293. // break;
  294. // case 1:
  295. // {
  296. // [searchSection setSectionTitle:@"推冷"];
  297. // [searchSection setButtonTitle:@"换一批"];
  298. // WEAKSELF
  299. // searchSection.rightAction = ^{
  300. // STRONGSELF
  301. // if (section == 0 && section == 2) {
  302. // return ;
  303. // }
  304. // [strongSelf getData2:2 page:strongSelf.coldPage + 1 perPage:3];
  305. //// strongSelf.coldArray = @[strongSelf.coldArray[2],strongSelf.coldArray[0],strongSelf.coldArray[1]];
  306. //// [tableView reloadSection:section withRowAnimation:UITableViewRowAnimationAutomatic];
  307. // };
  308. // }
  309. // break;
  310. // case 2:
  311. // {
  312. // [searchSection setSectionTitle:@"最近搜索"];
  313. // WEAKSELF
  314. // searchSection.rightAction = ^{
  315. // // 清空历史记录
  316. // STRONGSELF
  317. // if (section == 0 && section == 1) {
  318. // return ;
  319. // }
  320. // [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:@[]];
  321. // strongSelf.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]];
  322. // [tableView reloadData];
  323. // };
  324. // }
  325. // break;
  326. // default:
  327. // break;
  328. // }
  329. // return searchSection;
  330. //}
  331. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  332. if ([tableView isEqual:self.historyTableView]) {
  333. return 0;
  334. }else {
  335. return self.resultArray.count;
  336. }
  337. }
  338. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  339. if ([tableView isEqual:self.historyTableView]) {
  340. return 34;
  341. }else {
  342. Item *model = self.resultArray[indexPath.row];
  343. if (model.SourceType == 0) {
  344. // UILabel * label = [UILabel new];
  345. // label.numberOfLines = 2;
  346. // label.text = model.Summary;
  347. // CGFloat H = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT)].height;
  348. // NSLog(@"height:%f",H);
  349. // return 86 + H + (H > 36 ? 15 : 0);
  350. return UITableViewAutomaticDimension;
  351. }else{
  352. return 159.f;
  353. }
  354. }
  355. }
  356. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  357. if ([tableView isEqual:self.historyTableView]) {
  358. SearchHistoryCell * searchHistoryCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"SearchHistoryCell"];
  359. if (!searchHistoryCell) {
  360. searchHistoryCell = [[SearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchHistoryCell"];
  361. }
  362. [searchHistoryCell setCurrentHistoryData:_historyArray[indexPath.row]];
  363. // WEAKSELF
  364. // searchHistoryCell.deleteAction = ^{
  365. // // 删除单条历史记录
  366. // STRONGSELF
  367. // [strongSelf.historyArray removeObjectAtIndex:indexPath.row];
  368. // [_TDFileStorage saveMainSearchArray:strongSelf.historyArray];
  369. // [tableView reloadData];
  370. // };
  371. // searchHistoryCell.searchAction = ^(NSString *searchStr) {
  372. // // 点击某个历史记录
  373. // STRONGSELF
  374. // self.searchStr = searchStr;
  375. // [strongSelf.topNavSearch setNowSearchData:self.searchStr];
  376. // [self getListData:searchStr IsClickSearchAction:YES];
  377. // };
  378. return searchHistoryCell;
  379. }
  380. // else if ([tableView isEqual:self.thinkTableView]) {
  381. // Item *tmpItem = self.thinkArray[indexPath.row];
  382. // UITableViewCell * thinkCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:@"thinkCell"];
  383. // if (!thinkCell) {
  384. // thinkCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"thinkCell"];
  385. // thinkCell.textLabel.font = Kfont(14);
  386. // thinkCell.textLabel.textColor = kColorFromRGB(0x999999);
  387. // }
  388. // thinkCell.selectionStyle = UITableViewCellSelectionStyleNone;
  389. // thinkCell.textLabel.attributedText = [ZYCTool checkOfString:tmpItem.Title withSearchText:self.searchStr];
  390. // return thinkCell;
  391. // }
  392. else {
  393. Item *model = self.resultArray[indexPath.row];
  394. if (model.SourceType == 0) {
  395. SearchDetailViewCell * cell = [SearchDetailViewCell configCell:tableView indexPath:indexPath];
  396. [cell loadCurrentData:model hotWord:self.searchStr];
  397. return cell;
  398. }else{
  399. MyApprovalBCell *cell = (MyApprovalBCell *)[tableView dequeueReusableCellWithIdentifier:MyApprovalBCellID];
  400. [cell loadSearchData:model];
  401. cell.falgLab.hidden = YES;
  402. return cell;
  403. }
  404. }
  405. // UITableViewCell *cell;
  406. // if (indexPath.section == 2) {
  407. // NSString *cellIdentifier = @"SearchHistoryCell";
  408. // SearchHistoryCell * searchHistoryCell = (SearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  409. // if (!searchHistoryCell) {
  410. // searchHistoryCell = [[SearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  411. // }
  412. // [searchHistoryCell setCurrentHistoryData:_historyArray[indexPath.row]];
  413. // WEAKSELF
  414. // searchHistoryCell.deleteAction = ^{
  415. // // 清空搜索历史记录
  416. // STRONGSELF
  417. // [strongSelf.historyArray removeObjectAtIndex:indexPath.row];
  418. // [_TDFileStorage saveMainSearchArray:strongSelf.historyArray];
  419. // [tableView reloadData];
  420. // };
  421. // searchHistoryCell.searchAction = ^(NSString *searchStr) {
  422. // STRONGSELF
  423. // [strongSelf.topNavSearch setNowSearchData:searchStr];
  424. // [strongSelf goDetailController:searchStr];
  425. // };
  426. // cell = searchHistoryCell;
  427. // } else {
  428. // NSString *cellIdentifier = @"SearchTypeCell";
  429. // SearchTypeCell * searchTypeCell = (SearchTypeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  430. // if (!searchTypeCell) {
  431. // searchTypeCell = [[SearchTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  432. // }
  433. // if (!indexPath.section) {
  434. // [searchTypeCell setCurrentDataArray:_warmArray];
  435. // } else {
  436. // [searchTypeCell setCurrentDataArray:_coldArray];
  437. // }
  438. // WEAKSELF
  439. // searchTypeCell.seachAction = ^(NSString *searchStr){
  440. // STRONGSELF
  441. // [strongSelf.topNavSearch setNowSearchData:searchStr];
  442. // [strongSelf goDetailController:searchStr];
  443. // };
  444. // cell = searchTypeCell;
  445. // }
  446. // cell.selectionStyle = UITableViewCellSelectionStyleNone;
  447. // return cell;
  448. }
  449. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  450. [self.view endEditing:YES];
  451. if ([tableView isEqual:self.historyTableView]) {
  452. self.searchStr = self.historyArray[indexPath.row];
  453. [self.topNavSearch setNowSearchData:self.searchStr];
  454. [self getListData:self.searchStr IsClickSearchAction:YES];
  455. // }
  456. // else if ([tableView isEqual:self.thinkTableView]) {
  457. // HomeDetailController *homeDetail = [[HomeDetailController alloc] init];
  458. // [homeDetail loadCurrentModel:self.thinkArray[indexPath.row]];
  459. // [self.navigationController pushViewController:homeDetail animated:YES];
  460. } else {
  461. Item *model = self.resultArray[indexPath.row];
  462. if (model.SourceType == 0) {
  463. // HomeDetailController *homeDetail = [[HomeDetailController alloc] init];
  464. // [homeDetail loadCurrentModel:self.thinkArray[indexPath.row]];
  465. // [self.navigationController pushViewController:homeDetail animated:YES];
  466. Item *itemModel = self.thinkArray[indexPath.row];
  467. MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
  468. vc.type = CollectModel_Aritle;
  469. vc.Id = itemModel.Id;
  470. [self.navigationController pushViewController:vc animated:YES];
  471. }else{
  472. MyApprovalPageDetail * myApprovalPageDetail = [[MyApprovalPageDetail alloc]init];
  473. myApprovalPageDetail.indexId = model.FlowId;
  474. myApprovalPageDetail.title = model.Title;
  475. myApprovalPageDetail.TodoId = model.Id;
  476. myApprovalPageDetail.userId = model.UserId;
  477. if ([model.Type isEqualToString:@"ApproverCheck"]) {
  478. myApprovalPageDetail.pageType = Type_ONEA;
  479. }else{
  480. myApprovalPageDetail.pageType = Type_ONEC;
  481. }
  482. [self.navigationController pushViewController:myApprovalPageDetail animated:YES];
  483. }
  484. }
  485. }
  486. #pragma mark - 查重
  487. - (void)insertHistorySearchStr:(NSString *)string {
  488. for (NSString *cache in _historyArray) {
  489. // 查重
  490. if ([cache isEqualToString:string]) {
  491. return;
  492. }
  493. }
  494. [_historyArray insertObject:string atIndex:0];
  495. if (_historyArray.count > 10) {
  496. NSArray *orderArray = [_historyArray subarrayWithRange:NSMakeRange(0, 10)];
  497. _historyArray = orderArray.mutableCopy;
  498. }
  499. [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:_historyArray];
  500. }
  501. #pragma mark - getter
  502. - (InfoSearchView *)topNavSearch {
  503. if (!_topNavSearch) {
  504. _topNavSearch = [[InfoSearchView alloc] initWithFrame:CGRectMake(0, (IS_IPHONEX?40:20), kGXScreenWidth, 45) type:InfoSearchViewTypeDefault];
  505. _topNavSearch.delegate = self;
  506. WEAKSELF
  507. _topNavSearch.backBlock = ^{
  508. [weakSelf.navigationController popViewControllerAnimated:YES];
  509. };
  510. _topNavSearch.menuBlock = ^{
  511. [weakSelf.view endEditing:YES];
  512. [[UtilsTools getWindow] addSubview:weakSelf.drawerView];
  513. weakSelf.drawerView.SelectDrawerBlock = ^(NSIndexPath * _Nonnull indexPath) {
  514. if (![weakSelf.tabBarController isKindOfClass:[TabBarController class]]) {
  515. [TabBarController sharedTabBarController].tabBar.hidden = NO;
  516. [weakSelf.tabBarController.navigationController popViewControllerAnimated:NO];
  517. }
  518. if ([weakSelf.navigationController.viewControllers count] > 1) {
  519. [weakSelf.navigationController popToRootViewControllerAnimated:NO];
  520. }
  521. switch (indexPath.section) {
  522. case 0:
  523. {
  524. [[TabBarController sharedTabBarController] setSelectedIndex:indexPath.row];
  525. }
  526. break;
  527. case 1:
  528. {
  529. switch (indexPath.row) {
  530. case 0:
  531. {
  532. [[TabBarController sharedTabBarController] setSelectedIndex:2];
  533. }
  534. break;
  535. case 1:
  536. {
  537. [[TabBarController sharedTabBarController] setSelectedIndex:2];
  538. }
  539. break;
  540. default:
  541. {
  542. [[TabBarController sharedTabBarController] setSelectedIndex:3];
  543. }
  544. break;
  545. }
  546. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  547. [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(indexPath.row)}];
  548. });
  549. }
  550. break;
  551. default:
  552. {
  553. [[TabBarController sharedTabBarController] setSelectedIndex:3];
  554. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  555. [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(4 + indexPath.row)}];
  556. });
  557. }
  558. break;
  559. }
  560. };
  561. weakSelf.drawerView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);// - kNavigationHeight
  562. };
  563. }
  564. return _topNavSearch;
  565. }
  566. - (TDTableView *)historyTableView {
  567. if (!_historyTableView) {
  568. _historyTableView = [[TDTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  569. _historyTableView.dataSource = self;
  570. _historyTableView.delegate = self;
  571. _historyTableView.frame = CGRectMake(0, (IS_IPHONEX?40:20)+45, kGXScreenWidth, kGXScreenHeigh-(IS_IPHONEX?40:20)-45);
  572. }
  573. return _historyTableView;
  574. }
  575. //- (TDTableView *)thinkTableView {
  576. // if (!_thinkTableView) {
  577. // _thinkTableView = [[TDTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  578. // _thinkTableView.dataSource = self;
  579. // _thinkTableView.delegate = self;
  580. // _thinkTableView.frame = CGRectMake(0, (IS_IPHONEX?40:20)+45, kGXScreenWidth, kGXScreenHeigh-(IS_IPHONEX?40:20)-45);
  581. // [_thinkTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
  582. // }
  583. // return _thinkTableView;
  584. //}
  585. - (UITableView *)resultTableView {
  586. if (!_resultTableView) {
  587. _resultTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  588. _resultTableView.dataSource = self;
  589. _resultTableView.delegate = self;
  590. _resultTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  591. }
  592. return _resultTableView;
  593. }
  594. - (NSMutableArray *)historyArray {
  595. if (!_historyArray) {
  596. _historyArray = [_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home].mutableCopy;
  597. }
  598. return _historyArray;
  599. }
  600. - (NSMutableArray<Item *> *)thinkArray {
  601. if (!_thinkArray) {
  602. _thinkArray = [NSMutableArray<Item *> array];
  603. }
  604. return _thinkArray;
  605. }
  606. - (NSMutableArray<Item *> *)resultArray {
  607. if (!_resultArray) {
  608. _resultArray = [NSMutableArray<Item *> array];
  609. }
  610. return _resultArray;
  611. }
  612. - (UIView *)noDataView {
  613. if (!_noDataView) {
  614. _noDataView = [UIView new];
  615. _noDataView.backgroundColor = [UIColor whiteColor];
  616. }
  617. return _noDataView;
  618. }
  619. - (UILabel *)noDataTipLabel {
  620. if (!_noDataTipLabel) {
  621. _noDataTipLabel = [UILabel new];
  622. _noDataTipLabel.text = @"暂无搜索结果";
  623. _noDataTipLabel.font = Kfont(15);
  624. _noDataTipLabel.textColor = kColorFromRGB(0x999999);
  625. _noDataTipLabel.textAlignment = NSTextAlignmentCenter;
  626. }
  627. return _noDataTipLabel;
  628. }
  629. - (UIView *)historyTableHeaderView {
  630. if (!_historyTableHeaderView) {
  631. _historyTableHeaderView = [UIView new];
  632. _historyTableHeaderView.backgroundColor = [UIColor whiteColor];;
  633. }
  634. return _historyTableHeaderView;
  635. }
  636. - (SearchTypeSection *)historyHeaderTitleView {
  637. if (!_historyHeaderTitleView) {
  638. _historyHeaderTitleView = [[SearchTypeSection alloc] initWithFrame:CGRectMake(0, 0, kGXScreenWidth, 44)];
  639. }
  640. return _historyHeaderTitleView;
  641. }
  642. #pragma mark - historyHeaderView
  643. // 添加tagView
  644. - (void)addAndLayoutTagsWithTagsContentView:(UIView *)contentView tagTexts:(NSArray<NSString *> *)tagTexts;
  645. {
  646. [contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  647. NSMutableArray *tagsM = [NSMutableArray array];
  648. for (int i = 0; i < tagTexts.count; i++) {
  649. UILabel *label = [self labelWithTitle:tagTexts[i]];
  650. [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagDidCLick:)]];
  651. [contentView addSubview:label];
  652. [tagsM addObject:label];
  653. }
  654. CGFloat currentX = 0;
  655. CGFloat currentY = 0;
  656. CGFloat countRow = 0;
  657. CGFloat countCol = 0;
  658. CGFloat leftMargin = 12;
  659. for (int i = 0; i < contentView.subviews.count; i++) {
  660. UILabel *subView = contentView.subviews[i];
  661. // When the number of search words is too large, the width is width of the contentView
  662. if (subView.width > contentView.width) {
  663. subView.width = contentView.width;
  664. }
  665. if (leftMargin + currentX + subView.width + PYSEARCH_MARGIN * countRow > contentView.width) {
  666. subView.x = leftMargin;
  667. subView.y = (currentY += subView.height) + PYSEARCH_MARGIN * ++countCol;
  668. currentX = subView.width;
  669. countRow = 1;
  670. } else {
  671. subView.x = leftMargin + (currentX += subView.width) - subView.width + PYSEARCH_MARGIN * countRow;
  672. subView.y = currentY + PYSEARCH_MARGIN * countCol;
  673. countRow ++;
  674. }
  675. }
  676. }
  677. // 创建label
  678. - (UILabel *)labelWithTitle:(NSString *)title
  679. {
  680. UILabel *label = [[UILabel alloc] init];
  681. label.userInteractionEnabled = YES;
  682. label.font = [UIFont systemFontOfSize:14];
  683. label.text = title;
  684. label.textColor = [UIColor grayColor];
  685. label.backgroundColor = kColorFromRGB(0xfafafa);
  686. label.textAlignment = NSTextAlignmentCenter;
  687. [label sizeToFit];
  688. label.width += 20;
  689. label.height += 14;
  690. label.layer.cornerRadius = label.height / 2;
  691. label.clipsToBounds = YES;
  692. return label;
  693. }
  694. - (void)tagDidCLick:(UITapGestureRecognizer *)gr
  695. {
  696. UILabel *label = (UILabel *)gr.view;
  697. self.searchStr = label.text;
  698. [self.topNavSearch setNowSearchData:self.searchStr];
  699. [self getListData:self.searchStr IsClickSearchAction:YES];
  700. }
  701. - (UIView *)historyHeaderContainer {
  702. if (!_historyHeaderContainer) {
  703. _historyHeaderContainer = [UIView new];
  704. _historyHeaderContainer.backgroundColor = [UIColor whiteColor];
  705. }
  706. return _historyHeaderContainer;
  707. }
  708. // 创建historyTableView的headerView
  709. - (void)initHistoryTableHeaderView {
  710. [self.historyHeaderTitleView setSectionTitle:@"最近搜索"];
  711. WEAKSELF
  712. self.historyHeaderTitleView.rightAction = ^{
  713. UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  714. UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  715. // 清空历史记录
  716. [_TDFileStorage saveHistorySearchBySearchType:HistorySearchType_Home WithArr:@[]];
  717. weakSelf.historyArray = [NSMutableArray arrayWithArray:[_TDFileStorage loadHistorySearchBySearchType:HistorySearchType_Home]];
  718. [weakSelf addAndLayoutTagsWithTagsContentView:weakSelf.historyTableHeaderView tagTexts:weakSelf.historyArray];
  719. [weakSelf resetHistoryHeaderViewHeight];
  720. if (weakSelf.historyArray.count == 0) {
  721. weakSelf.hisNoView.hidden = NO;
  722. }else{
  723. weakSelf.hisNoView.hidden = YES;
  724. }
  725. [weakSelf.historyTableView reloadData];
  726. }];
  727. UIAlertController *tmpVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除最近搜索?" preferredStyle:UIAlertControllerStyleAlert];
  728. [tmpVc addAction:action1];
  729. [tmpVc addAction:action2];
  730. [weakSelf presentViewController:tmpVc animated:YES completion:nil];
  731. };
  732. [self.historyHeaderContainer addSubview:self.historyHeaderTitleView];
  733. self.historyTableHeaderView.frame = CGRectMake(0, self.historyHeaderTitleView.height + 13, kScreenWidth, 0);
  734. [self addAndLayoutTagsWithTagsContentView:self.historyTableHeaderView tagTexts:self.historyArray];
  735. CGFloat height = CGRectGetMaxY(self.historyTableHeaderView.subviews.lastObject.frame);
  736. self.historyTableHeaderView.frame = CGRectMake(0, self.historyHeaderTitleView.height + 13, kScreenWidth, height);
  737. [self.historyHeaderContainer addSubview:self.historyTableHeaderView];
  738. [self resetHistoryHeaderViewHeight];
  739. self.historyTableView.tableHeaderView = self.historyHeaderContainer;
  740. self.historyTableView.tableFooterView = self.hisNoView;
  741. if (self.historyArray.count == 0) {
  742. self.hisNoView.hidden = NO;
  743. }else{
  744. self.hisNoView.hidden = YES;
  745. }
  746. UILabel * label = [UILabel new];
  747. label.text = @"暂无搜索记录!";
  748. label.font = [UIFont systemFontOfSize:14];
  749. label.textColor = UIColorHex(0x999999);
  750. [self.hisNoView addSubview:label];
  751. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  752. make.center.mas_equalTo(self.hisNoView);
  753. }];
  754. }
  755. - (UIView *)hisNoView
  756. {
  757. if (!_hisNoView) {
  758. _hisNoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
  759. _hisNoView.hidden = YES;
  760. }
  761. return _hisNoView;
  762. }
  763. // 更新header高度
  764. - (void)resetHistoryHeaderViewHeight {
  765. CGFloat containerHeight = CGRectGetMaxY(self.historyHeaderContainer.subviews.lastObject.frame);
  766. self.historyHeaderContainer.frame = CGRectMake(0, 0, kScreenWidth, containerHeight);
  767. }
  768. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  769. {
  770. [self.topNavSearch.searchBar resignFirstResponder];
  771. }
  772. @end