Ei kuvausta

KXNewKeySearchViewController.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // KXNewKeySearchViewController.m
  3. // CAISHEN
  4. //
  5. // Created by kuxuan on 2017/12/5.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXNewKeySearchViewController.h"
  9. #import "KXKeyWordView.h"
  10. #import "KXNewsModel.h"
  11. #import "KXNewsTableViewCell.h"
  12. #import "KXKeyWordTool.h"
  13. #import "KXNewsWebViewController.h"
  14. #define BASE_PATH [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"searchHistory.archiver"]
  15. @interface KXNewKeySearchViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
  16. {
  17. UIButton *popButton;
  18. UITextField *searchTF;
  19. /**放大镜*/
  20. UIView *placeholderView;
  21. /**是否已经进行搜索*/
  22. BOOL _isSearch;
  23. UITableView *tableView;
  24. NSMutableArray *mArray;
  25. NSInteger page;
  26. }
  27. @property (nonatomic, strong) UITableView *searchTableView;
  28. @property (nonatomic, strong)NSMutableArray *dataSource;
  29. @property (nonatomic, assign)BOOL isSearch;
  30. @end
  31. @implementation KXNewKeySearchViewController
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Do any additional setup after loading the view.
  35. page = 1;
  36. self.isSearch = NO;
  37. if (![NSKeyedUnarchiver unarchiveObjectWithFile:BASE_PATH]) {
  38. mArray=[[NSMutableArray alloc]init];
  39. }else{
  40. mArray=[NSKeyedUnarchiver unarchiveObjectWithFile:BASE_PATH];
  41. }
  42. [self createNav];
  43. [self createTableView];
  44. [self createSearchTableView];
  45. }
  46. -(void)createNav
  47. {
  48. [self addLeftBarButtonItemWithImageName:nil title:nil target:nil selector:nil];
  49. popButton=[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-50, (NavHeight - StatusBarHeight - 20)/2, 35, 20)];
  50. [popButton setTitle:@"取消" forState:UIControlStateNormal];
  51. [popButton setTitleColor:[UIColor KXColorWithHex:0x007aff] forState:UIControlStateNormal];
  52. popButton.titleLabel.font=FONT_SYS(16);
  53. [popButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  54. [self.navigationController.navigationBar addSubview:popButton];
  55. /*
  56. *搜索框
  57. */
  58. searchTF=[[UITextField alloc]initWithFrame:CGRectMake(14, (NavHeight - StatusBarHeight - 28)/2, SCREEN_WIDTH-75, 28)];
  59. searchTF.layer.cornerRadius=14;
  60. searchTF.layer.masksToBounds=YES;
  61. searchTF.font=FONT_SYS(14);
  62. searchTF.clearButtonMode=UITextFieldViewModeAlways;
  63. searchTF.borderStyle=UITextBorderStyleNone;
  64. searchTF.backgroundColor=[UIColor KXColorWithHex:0xf8f8f8];
  65. searchTF.keyboardType=UIKeyboardTypeDefault;
  66. searchTF.returnKeyType=UIReturnKeySearch;
  67. searchTF.delegate=self;
  68. [self.navigationController.navigationBar addSubview:searchTF];
  69. placeholderView=[[UIView alloc]initWithFrame:CGRectMake(14, 2, 35, 24)];
  70. [searchTF addSubview:placeholderView];
  71. UIImageView *glassImageView=[[UIImageView alloc]initWithFrame:CGRectMake(14, 1, 22, 22)];
  72. glassImageView.image=[UIImage imageNamed:@"main_search"];
  73. [placeholderView addSubview:glassImageView];
  74. searchTF.leftView=placeholderView;
  75. searchTF.leftViewMode=UITextFieldViewModeAlways;
  76. searchTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  77. searchTF.placeholder=@"请输入搜索关键字";
  78. }
  79. -(void)backAction
  80. {
  81. [searchTF resignFirstResponder];
  82. [searchTF removeFromSuperview];
  83. [popButton removeFromSuperview];
  84. [placeholderView removeFromSuperview];
  85. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  86. self.navigationController.navigationBar.hidden=NO;
  87. CATransition * animation = [CATransition animation];
  88. animation.duration = 0.5; // 时间
  89. animation.type = kCATransitionFade;
  90. animation.subtype = kCATransitionFromTop;
  91. [self.view.window.layer addAnimation:animation forKey:nil];
  92. [self.navigationController popViewControllerAnimated:YES];
  93. }
  94. - (void)createTableView
  95. {
  96. tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NavHeight) style:UITableViewStylePlain];
  97. tableView.backgroundColor = [UIColor KXColorWithHex:0xe6e6ec];
  98. tableView.delegate = self;
  99. tableView.dataSource = self;
  100. [self.view addSubview:tableView];
  101. if (mArray.count>0) {
  102. [self createTableViewHeader];
  103. }
  104. KXKeyWordView *keyword = [[KXKeyWordView alloc]init];
  105. __weak typeof(self)weakself = self;
  106. [KXKeyWordTool requestKeyword:^(NSArray *array) {
  107. dispatch_async(dispatch_get_main_queue(), ^{
  108. keyword.itemArray = array;
  109. keyword.keywordBlock = ^(NSString *keyword) {
  110. searchTF.text = keyword;
  111. [weakself searchKeyword];
  112. };
  113. tableView.tableFooterView = keyword;
  114. });
  115. }];
  116. }
  117. - (void)createTableViewHeader
  118. {
  119. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 42)];
  120. UILabel *keyWord = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 200, 42)];
  121. keyWord.text = @"搜索历史:";
  122. headerView.backgroundColor = [UIColor whiteColor];
  123. keyWord.textColor = [UIColor KXColorWithHex:0x9a9a9a];
  124. keyWord.font = FONT_SYS(14);
  125. [headerView addSubview:keyWord];
  126. UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  127. [deleteButton setImage:[UIImage imageNamed:@"main_search_delete"] forState:UIControlStateNormal];
  128. deleteButton.frame = CGRectMake(SCREEN_WIDTH - 36, 10, 22, 22);
  129. [deleteButton addTarget:self action:@selector(deleteHistory) forControlEvents:UIControlEventTouchUpInside];
  130. [headerView addSubview:deleteButton];
  131. tableView.tableHeaderView = headerView;
  132. }
  133. - (void)createSearchTableView
  134. {
  135. self.searchTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight) style:UITableViewStylePlain];
  136. self.searchTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
  137. self.searchTableView.hidden = YES;
  138. [self.searchTableView registerClass:[KXNewsTableViewCell class] forCellReuseIdentifier:@"news"];
  139. self.searchTableView.tableFooterView=[[UIView alloc]init];
  140. self.searchTableView.delegate = self;
  141. self.searchTableView.dataSource = self;
  142. [self.view addSubview:self.searchTableView];
  143. //下拉刷新
  144. MJRefreshNormalHeader *header =
  145. [[MJRefreshNormalHeader alloc]init];
  146. [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)];
  147. self.searchTableView.mj_header = header;
  148. //上拉加载
  149. MJRefreshAutoNormalFooter *footer=[[MJRefreshAutoNormalFooter alloc]init];
  150. [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)];
  151. self.searchTableView.mj_footer=footer;
  152. header.stateLabel.font = FONT_SYS(14);
  153. header.stateLabel.textColor=[UIColor detailTitleColor];
  154. header.lastUpdatedTimeLabel.font=FONT_SYS(14);
  155. header.lastUpdatedTimeLabel.textColor=[UIColor detailTitleColor];
  156. footer.stateLabel.textColor=[UIColor detailTitleColor];
  157. }
  158. #pragma mark 下拉刷新和上拉加载
  159. -(void)refreshWithHeader:(MJRefreshHeader *)header
  160. {
  161. page=1;
  162. [self requestSearchList];
  163. }
  164. -(void)refreshFooter:(MJRefreshAutoFooter *)footer
  165. {
  166. page++;
  167. [self requestSearchList];
  168. }
  169. - (void)requestSearchList
  170. {
  171. NSString *urlString = [NSString stringWithFormat:@"%@/Information/searchlist",URL];
  172. NSDictionary *para = @{@"word":searchTF.text,@"pageId":@(page)};
  173. [KXHTTP post:urlString params:para success:^(id json) {
  174. if (page==1) {
  175. [self.dataSource removeAllObjects];
  176. }
  177. NSArray *array=[NSArray yy_modelArrayWithClass:[KXNewsModel class] json:json[@"list"]];
  178. @synchronized (self.dataSource) {
  179. for (KXNewsModel *model in array) {
  180. [self.dataSource addObject:model];
  181. }
  182. }
  183. [self.searchTableView.mj_header endRefreshing];
  184. [self.searchTableView.mj_footer endRefreshing];
  185. [self.searchTableView reloadData];
  186. } failure:^(NSError *error) {
  187. }];
  188. }
  189. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  190. {
  191. if ([tableView isEqual:self.searchTableView]) {
  192. NSLog(@"catch self.searchTableView");
  193. }
  194. if (self.isSearch == YES) {
  195. return self.dataSource.count;
  196. }
  197. return mArray.count;
  198. }
  199. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  200. {
  201. if (self.isSearch == YES) {
  202. KXNewsModel *model=self.dataSource[indexPath.row];
  203. KXNewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"news"];
  204. if (!cell) {
  205. cell=[[KXNewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"news"];
  206. }
  207. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  208. cell.newsModel=model;
  209. return cell;
  210. }else{
  211. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  212. if (!cell) {
  213. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  214. }
  215. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  216. cell.textLabel.text = mArray[indexPath.row];
  217. return cell;
  218. }
  219. }
  220. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  221. {
  222. if (self.isSearch == YES) {
  223. KXNewsModel *model=self.dataSource[indexPath.row];
  224. KXNewsWebViewController *web=[[KXNewsWebViewController alloc]init];
  225. web.webID=model.Id;
  226. [self.navigationController pushViewController:web animated:YES];
  227. }else{
  228. searchTF.text = mArray[indexPath.row];
  229. [self searchKeyword];
  230. }
  231. }
  232. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  233. {
  234. if (self.isSearch == YES) {
  235. return 100;
  236. }
  237. return 42;
  238. }
  239. - (void)deleteHistory
  240. {
  241. [mArray removeAllObjects];
  242. [tableView reloadData];
  243. tableView.tableHeaderView = [[UITableView alloc]init];
  244. [NSKeyedArchiver archiveRootObject:mArray toFile:BASE_PATH];
  245. }
  246. -(BOOL)textFieldShouldReturn:(UITextField *)textField
  247. {
  248. [self searchKeyword];
  249. return YES;
  250. }
  251. - (void)searchKeyword
  252. {
  253. self.isSearch = YES;
  254. tableView.hidden = YES;
  255. self.searchTableView.hidden = NO;
  256. [self.searchTableView reloadData];
  257. [self.searchTableView.mj_header beginRefreshing];
  258. [searchTF resignFirstResponder];
  259. for (int i=0; i<mArray.count; i++) {
  260. NSString *search=mArray[i];
  261. if ([search isEqualToString:searchTF.text]) {
  262. [mArray removeObjectAtIndex:i];
  263. }
  264. }
  265. [mArray insertObject:searchTF.text atIndex:0];
  266. if (mArray.count > 5) {
  267. [mArray removeObjectsInRange:NSMakeRange(5, mArray.count - 5)];
  268. }
  269. [NSKeyedArchiver archiveRootObject:mArray toFile:BASE_PATH];
  270. }
  271. - (NSMutableArray *)dataSource
  272. {
  273. if (!_dataSource) {
  274. _dataSource = [[NSMutableArray alloc]init];
  275. }
  276. return _dataSource;
  277. }
  278. - (void)didReceiveMemoryWarning {
  279. [super didReceiveMemoryWarning];
  280. // Dispose of any resources that can be recreated.
  281. }
  282. /*
  283. #pragma mark - Navigation
  284. // In a storyboard-based application, you will often want to do a little preparation before navigation
  285. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  286. // Get the new view controller using [segue destinationViewController].
  287. // Pass the selected object to the new view controller.
  288. }
  289. */
  290. @end