HomeSearchController.m 31 KB

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