123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- //
- // KXNewKeySearchViewController.m
- // CAISHEN
- //
- // Created by kuxuan on 2017/12/5.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "KXNewKeySearchViewController.h"
- #import "KXKeyWordView.h"
- #import "KXNewsModel.h"
- #import "KXNewsTableViewCell.h"
- #import "KXKeyWordTool.h"
- #import "KXNewsWebViewController.h"
- #define BASE_PATH [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"searchHistory.archiver"]
- @interface KXNewKeySearchViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
- {
- UIButton *popButton;
- UITextField *searchTF;
- /**放大镜*/
- UIView *placeholderView;
- /**是否已经进行搜索*/
- BOOL _isSearch;
- UITableView *tableView;
- NSMutableArray *mArray;
- NSInteger page;
- }
- @property (nonatomic, strong) UITableView *searchTableView;
- @property (nonatomic, strong)NSMutableArray *dataSource;
- @property (nonatomic, assign)BOOL isSearch;
- @end
- @implementation KXNewKeySearchViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- page = 1;
- self.isSearch = NO;
- if (![NSKeyedUnarchiver unarchiveObjectWithFile:BASE_PATH]) {
- mArray=[[NSMutableArray alloc]init];
- }else{
- mArray=[NSKeyedUnarchiver unarchiveObjectWithFile:BASE_PATH];
- }
- [self createNav];
- [self createTableView];
- [self createSearchTableView];
- }
- -(void)createNav
- {
- [self addLeftBarButtonItemWithImageName:nil title:nil target:nil selector:nil];
- popButton=[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-50, (NavHeight - StatusBarHeight - 20)/2, 35, 20)];
- [popButton setTitle:@"取消" forState:UIControlStateNormal];
- [popButton setTitleColor:[UIColor KXColorWithHex:0x007aff] forState:UIControlStateNormal];
- popButton.titleLabel.font=FONT_SYS(16);
- [popButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
- [self.navigationController.navigationBar addSubview:popButton];
- /*
- *搜索框
- */
- searchTF=[[UITextField alloc]initWithFrame:CGRectMake(14, (NavHeight - StatusBarHeight - 28)/2, SCREEN_WIDTH-75, 28)];
- searchTF.layer.cornerRadius=14;
- searchTF.layer.masksToBounds=YES;
- searchTF.font=FONT_SYS(14);
- searchTF.clearButtonMode=UITextFieldViewModeAlways;
- searchTF.borderStyle=UITextBorderStyleNone;
- searchTF.backgroundColor=[UIColor KXColorWithHex:0xf8f8f8];
- searchTF.keyboardType=UIKeyboardTypeDefault;
- searchTF.returnKeyType=UIReturnKeySearch;
- searchTF.delegate=self;
- [self.navigationController.navigationBar addSubview:searchTF];
-
- placeholderView=[[UIView alloc]initWithFrame:CGRectMake(14, 2, 35, 24)];
- [searchTF addSubview:placeholderView];
-
- UIImageView *glassImageView=[[UIImageView alloc]initWithFrame:CGRectMake(14, 1, 22, 22)];
- glassImageView.image=[UIImage imageNamed:@"main_search"];
- [placeholderView addSubview:glassImageView];
-
- searchTF.leftView=placeholderView;
- searchTF.leftViewMode=UITextFieldViewModeAlways;
- searchTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
-
- searchTF.placeholder=@"请输入搜索关键字";
- }
- -(void)backAction
- {
- [searchTF resignFirstResponder];
- [searchTF removeFromSuperview];
- [popButton removeFromSuperview];
- [placeholderView removeFromSuperview];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
- self.navigationController.navigationBar.hidden=NO;
- CATransition * animation = [CATransition animation];
- animation.duration = 0.5; // 时间
- animation.type = kCATransitionFade;
- animation.subtype = kCATransitionFromTop;
- [self.view.window.layer addAnimation:animation forKey:nil];
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)createTableView
- {
- tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NavHeight) style:UITableViewStylePlain];
- tableView.backgroundColor = [UIColor KXColorWithHex:0xe6e6ec];
- tableView.delegate = self;
- tableView.dataSource = self;
- [self.view addSubview:tableView];
-
- if (mArray.count>0) {
- [self createTableViewHeader];
- }
-
- KXKeyWordView *keyword = [[KXKeyWordView alloc]init];
- __weak typeof(self)weakself = self;
- [KXKeyWordTool requestKeyword:^(NSArray *array) {
- dispatch_async(dispatch_get_main_queue(), ^{
- keyword.itemArray = array;
- keyword.keywordBlock = ^(NSString *keyword) {
- searchTF.text = keyword;
- [weakself searchKeyword];
- };
- tableView.tableFooterView = keyword;
- });
- }];
-
- }
- - (void)createTableViewHeader
- {
- UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 42)];
- UILabel *keyWord = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 200, 42)];
- keyWord.text = @"搜索历史:";
- headerView.backgroundColor = [UIColor whiteColor];
- keyWord.textColor = [UIColor KXColorWithHex:0x9a9a9a];
- keyWord.font = FONT_SYS(14);
- [headerView addSubview:keyWord];
-
- UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [deleteButton setImage:[UIImage imageNamed:@"main_search_delete"] forState:UIControlStateNormal];
- deleteButton.frame = CGRectMake(SCREEN_WIDTH - 36, 10, 22, 22);
- [deleteButton addTarget:self action:@selector(deleteHistory) forControlEvents:UIControlEventTouchUpInside];
- [headerView addSubview:deleteButton];
- tableView.tableHeaderView = headerView;
- }
- - (void)createSearchTableView
- {
- self.searchTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight) style:UITableViewStylePlain];
- self.searchTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
- self.searchTableView.hidden = YES;
- [self.searchTableView registerClass:[KXNewsTableViewCell class] forCellReuseIdentifier:@"news"];
- self.searchTableView.tableFooterView=[[UIView alloc]init];
- self.searchTableView.delegate = self;
- self.searchTableView.dataSource = self;
- [self.view addSubview:self.searchTableView];
- //下拉刷新
- MJRefreshNormalHeader *header =
- [[MJRefreshNormalHeader alloc]init];
- [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)];
- self.searchTableView.mj_header = header;
- //上拉加载
- MJRefreshAutoNormalFooter *footer=[[MJRefreshAutoNormalFooter alloc]init];
- [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)];
- self.searchTableView.mj_footer=footer;
-
- header.stateLabel.font = FONT_SYS(14);
- header.stateLabel.textColor=[UIColor detailTitleColor];
- header.lastUpdatedTimeLabel.font=FONT_SYS(14);
- header.lastUpdatedTimeLabel.textColor=[UIColor detailTitleColor];
- footer.stateLabel.textColor=[UIColor detailTitleColor];
- }
- #pragma mark 下拉刷新和上拉加载
- -(void)refreshWithHeader:(MJRefreshHeader *)header
- {
- page=1;
- [self requestSearchList];
- }
- -(void)refreshFooter:(MJRefreshAutoFooter *)footer
- {
- page++;
- [self requestSearchList];
- }
- - (void)requestSearchList
- {
- NSString *urlString = [NSString stringWithFormat:@"%@/Information/searchlist",URL];
- NSDictionary *para = @{@"word":searchTF.text,@"pageId":@(page)};
- [KXHTTP post:urlString params:para success:^(id json) {
- if (page==1) {
- [self.dataSource removeAllObjects];
- }
- NSArray *array=[NSArray yy_modelArrayWithClass:[KXNewsModel class] json:json[@"list"]];
- @synchronized (self.dataSource) {
- for (KXNewsModel *model in array) {
- [self.dataSource addObject:model];
- }
- }
- [self.searchTableView.mj_header endRefreshing];
- [self.searchTableView.mj_footer endRefreshing];
- [self.searchTableView reloadData];
- } failure:^(NSError *error) {
-
- }];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if ([tableView isEqual:self.searchTableView]) {
- NSLog(@"catch self.searchTableView");
- }
- if (self.isSearch == YES) {
- return self.dataSource.count;
- }
- return mArray.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (self.isSearch == YES) {
- KXNewsModel *model=self.dataSource[indexPath.row];
- KXNewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"news"];
- if (!cell) {
- cell=[[KXNewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"news"];
- }
- cell.selectionStyle=UITableViewCellSelectionStyleNone;
- cell.newsModel=model;
- return cell;
- }else{
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
- if (!cell) {
- cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
- }
- cell.selectionStyle=UITableViewCellSelectionStyleNone;
- cell.textLabel.text = mArray[indexPath.row];
- return cell;
- }
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (self.isSearch == YES) {
- KXNewsModel *model=self.dataSource[indexPath.row];
- KXNewsWebViewController *web=[[KXNewsWebViewController alloc]init];
- web.webID=model.Id;
- [self.navigationController pushViewController:web animated:YES];
- }else{
- searchTF.text = mArray[indexPath.row];
- [self searchKeyword];
- }
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (self.isSearch == YES) {
- return 100;
- }
- return 42;
- }
- - (void)deleteHistory
- {
- [mArray removeAllObjects];
- [tableView reloadData];
- tableView.tableHeaderView = [[UITableView alloc]init];
- [NSKeyedArchiver archiveRootObject:mArray toFile:BASE_PATH];
- }
- -(BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- [self searchKeyword];
- return YES;
- }
- - (void)searchKeyword
- {
- self.isSearch = YES;
- tableView.hidden = YES;
- self.searchTableView.hidden = NO;
- [self.searchTableView reloadData];
- [self.searchTableView.mj_header beginRefreshing];
- [searchTF resignFirstResponder];
-
- for (int i=0; i<mArray.count; i++) {
- NSString *search=mArray[i];
- if ([search isEqualToString:searchTF.text]) {
- [mArray removeObjectAtIndex:i];
- }
- }
- [mArray insertObject:searchTF.text atIndex:0];
- if (mArray.count > 5) {
- [mArray removeObjectsInRange:NSMakeRange(5, mArray.count - 5)];
- }
-
- [NSKeyedArchiver archiveRootObject:mArray toFile:BASE_PATH];
-
- }
- - (NSMutableArray *)dataSource
- {
- if (!_dataSource) {
- _dataSource = [[NSMutableArray alloc]init];
- }
- return _dataSource;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|