酷店

KDPSearchViewController.m 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // KDPSearchViewController.m
  3. // KuDianProject
  4. //
  5. // Created by admin on 2019/7/10.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPSearchViewController.h"
  9. #import "MSSAutoresizeLabelFlow.h"
  10. #import "KDPSearchResultViewController.h"
  11. @interface KDPSearchViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
  12. @property (nonatomic, strong) UITableView *tableview;
  13. @property (nonatomic, strong) NSMutableArray *historyDataArray;
  14. @property (nonatomic, strong) NSMutableArray *searchArray;
  15. @property (nonatomic, strong) UIView *historyView;
  16. @property (nonatomic, strong) UITextField *searchTextField;
  17. @property (nonatomic, strong) MSSAutoresizeLabelFlow *flowView;
  18. @end
  19. @implementation KDPSearchViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. [self setUpNav];
  24. [self loadHisttoryData];
  25. [self setContentView];
  26. }
  27. - (void)setUpNav{
  28. [self.navBar setLineViewWithHidden:YES];
  29. self.navBar.backgroundColor = [UIColor colorWithHex:0xf9f9f9];
  30. self.view.backgroundColor = [UIColor whiteColor];
  31. UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(10, KDStatusHeight + 7, SCREEN_WIDTH-85, 31)];
  32. searchView.layer.cornerRadius = 16;
  33. searchView.layer.masksToBounds = YES;
  34. searchView.backgroundColor = [UIColor whiteColor];
  35. [self.navBar addSubview:searchView];
  36. UIImageView *searchIcon = [[UIImageView alloc] initWithFrame:CGRectMake(13, 8, 15, 15)];
  37. searchIcon.image = [UIImage imageNamed:@"search_icon"];
  38. [searchView addSubview:searchIcon];
  39. self.searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(searchIcon.right + 7, 4.5, searchView.width-45, 22)];
  40. self.searchTextField.delegate = self;
  41. self.searchTextField.font = FONT_SYS(14);
  42. self.searchTextField.textColor = [UIColor colorWithHex:0x8F8F95];
  43. self.searchTextField.returnKeyType = UIReturnKeySearch;
  44. self.searchTextField.backgroundColor = [UIColor clearColor];
  45. self.searchTextField.placeholder = @"搜索想要的内容";
  46. [searchView addSubview:self.searchTextField];
  47. UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  48. cancelBtn.frame = CGRectMake(SCREEN_WIDTH-55, 0, 35, 21);
  49. cancelBtn.centerY = searchView.centerY;
  50. [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  51. [cancelBtn setTitleColor:[UIColor colorWithHex:0x666666] forState:UIControlStateNormal];
  52. cancelBtn.titleLabel.font = FONT_SYS(15);
  53. [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
  54. [self.navBar addSubview:cancelBtn];
  55. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textchangeclick) name:UITextFieldTextDidChangeNotification object:nil];
  56. }
  57. - (void)loadHisttoryData{
  58. [self.historyDataArray removeAllObjects];
  59. NSArray *saveHistory = [NSKeyedUnarchiver unarchiveObjectWithFile:SearchPath];
  60. [self.historyDataArray addObjectsFromArray:saveHistory];
  61. }
  62. - (void)setContentView{
  63. [self.view addSubview:self.tableview];
  64. [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.edges.equalTo(UIEdgeInsetsMake(KDNavBarHeight, 0, 0, 0));
  66. }];
  67. [self.view addSubview:self.historyView];
  68. [self.historyView mas_makeConstraints:^(MASConstraintMaker *make) {
  69. make.edges.equalTo(UIEdgeInsetsZero);
  70. }];
  71. UILabel *searchHistLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, KDNavBarHeight + 10, 100, 30)];
  72. searchHistLabel.text = @"搜索历史";
  73. searchHistLabel.textColor = [UIColor colorWithHex:0x666666];
  74. searchHistLabel.font = FONT_SYS(14);
  75. [self.historyView addSubview:searchHistLabel];
  76. UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  77. [deleteBtn setTitle:@"清空" forState:UIControlStateNormal];
  78. [deleteBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
  79. [deleteBtn setImage:[UIImage imageNamed:@"delete_collection"] forState:UIControlStateNormal];
  80. [deleteBtn addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
  81. deleteBtn.titleLabel.font = FONT_SYS(13);
  82. [deleteBtn setButtonStyle:WSLButtonStyleImageLeft spacing:3];
  83. [self.historyView addSubview:deleteBtn];
  84. [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  85. make.top.equalTo(KDNavBarHeight + 10);
  86. make.right.equalTo(-20);
  87. make.height.equalTo(30);
  88. }];
  89. self.flowView = [[MSSAutoresizeLabelFlow alloc] initWithFrame:CGRectMake(0, searchHistLabel.bottom, self.view.width, 10) titles:self.historyDataArray selectedHandler:^(NSUInteger index, NSString *title) {
  90. [self skipToResultWithString:title];
  91. }];
  92. [self.historyView addSubview:self.flowView];
  93. self.historyView.hidden = self.historyDataArray.count == 0 ? YES : NO;
  94. }
  95. - (void)requestSearchData:(NSString *)keyWord{
  96. NSString *searchUrl = [NSString stringWithFormat:@"%@api/goods/searchAdvice",KDURL];
  97. [KDPNetworkRequestHTTP postURL:searchUrl params:@{@"keyword":keyWord} success:^(id _Nonnull json) {
  98. NSArray *data = json[@"data"];
  99. [self.searchArray removeAllObjects];
  100. [self.searchArray addObjectsFromArray:data];
  101. if (self.searchArray.count) {
  102. self.historyView.hidden = YES;
  103. self.tableview.hidden = NO;
  104. } else{
  105. self.historyView.hidden = NO;
  106. self.tableview.hidden = YES;
  107. }
  108. [self.tableview reloadData];
  109. } failure:^(NSError * _Nonnull error) {
  110. }];
  111. }
  112. - (void)skipToResultWithString:(NSString *)searchText{
  113. if (![self.historyDataArray containsObject:searchText]) {
  114. [self.historyDataArray addObject:searchText];
  115. }
  116. [self saveHistory];
  117. // 跳转到结果页
  118. KDPSearchResultViewController *searchResultVC = [[KDPSearchResultViewController alloc] init];
  119. searchResultVC.keyWordStr = searchText;
  120. [self.navigationController pushViewController:searchResultVC animated:NO];
  121. }
  122. - (void)saveHistory{
  123. BOOL success = [NSKeyedArchiver archiveRootObject:self.historyDataArray toFile:SearchPath];
  124. if (success) {
  125. NSLog(@"保存成功");
  126. [self.flowView reloadAllWithTitles:self.historyDataArray];
  127. }
  128. }
  129. - (void)cancelAction:(UIButton *)sender{
  130. [self.navigationController popViewControllerAnimated:NO];
  131. }
  132. - (void)textchangeclick{
  133. if (self.searchTextField.text.length>0) {
  134. [self requestSearchData:self.searchTextField.text];
  135. }
  136. }
  137. - (void)viewWillAppear:(BOOL)animated{
  138. [super viewWillAppear:animated];
  139. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  140. }
  141. - (void)viewWillDisappear:(BOOL)animated{
  142. [super viewWillDisappear:animated];
  143. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  144. }
  145. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  146. {
  147. if ( textField.text.length>0) {
  148. [self skipToResultWithString:textField.text];
  149. }
  150. return YES;
  151. }
  152. - (void)deleteAction{
  153. [self.historyDataArray removeAllObjects];
  154. [self saveHistory];
  155. [self.flowView reloadAllWithTitles:self.historyDataArray];
  156. self.historyView.hidden = YES;
  157. }
  158. #pragma UITableview delegate and datasource
  159. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  160. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchList"];
  161. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  162. if (!cell) {
  163. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchList"];
  164. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  165. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
  166. lineView.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
  167. [cell.contentView addSubview:lineView];
  168. }
  169. cell.textLabel.text = self.searchArray[indexPath.row];
  170. return cell;
  171. }
  172. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  173. return 1;
  174. }
  175. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  176. return self.searchArray.count;
  177. }
  178. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  179. [self skipToResultWithString:self.searchArray[indexPath.row]];
  180. }
  181. #pragma mark - 懒加载
  182. - (NSMutableArray *)historyDataArray{
  183. if (!_historyDataArray) {
  184. _historyDataArray = [NSMutableArray array];
  185. }
  186. return _historyDataArray;
  187. }
  188. - (NSMutableArray *)searchArray{
  189. if (!_searchArray) {
  190. _searchArray = [NSMutableArray array];
  191. }
  192. return _searchArray;
  193. }
  194. - (UITableView *)tableview{
  195. if (!_tableview) {
  196. _tableview = [[UITableView alloc] init];
  197. _tableview.delegate = self;
  198. _tableview.dataSource = self;
  199. _tableview.backgroundColor = [UIColor whiteColor];
  200. _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
  201. _tableview.hidden = YES;
  202. _tableview.showsVerticalScrollIndicator = NO;
  203. _tableview.showsHorizontalScrollIndicator = NO;
  204. _tableview.rowHeight = 50;
  205. [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"searchList"];
  206. }
  207. return _tableview;
  208. }
  209. - (UIView *)historyView{
  210. if (!_historyView) {
  211. _historyView = [[UIView alloc] init];
  212. _historyView.backgroundColor = [UIColor whiteColor];
  213. }
  214. return _historyView;
  215. }
  216. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  217. [self.view endEditing:YES];
  218. }
  219. @end