// // InvoiceStyleViewController.m // IBOSS // // Created by 关宏厚 on 2020/4/30. // Copyright © 2020 elongtian. All rights reserved. // #import "InvoiceStyleViewController.h" @interface InvoiceStyleViewController () @end @implementation InvoiceStyleViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"请选择"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0,45,22); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; self.navigationController.navigationBar.barTintColor = NavigationBarTintColor; self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:16]}; _tableView = [UITableView new]; _tableView.delegate=self; _tableView.dataSource=self; _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; _tableView.frame=CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height) ; [self.view addSubview:_tableView]; //搜索框 _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 44)]; _searchBar.delegate=self; self.tableView.tableHeaderView=_searchBar; } /** viewWillAppear函数 */ - (void)viewWillAppear:(BOOL)animated { _arrSearch = [[NSMutableArray alloc]init]; _arrFilter = [[NSMutableArray alloc]init]; [self reloadData]; } -(void)reloadData { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"GetInvoiceLayout" forKey:@"Action"]; [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; _mDownManager = [[ASIDownManager alloc] init]; [self startLoading]; _mDownManager.delegate = self; _mDownManager.onRequestSuccess = @selector(onInvoiceStyleLoadFinish:); _mDownManager.onRequestFail = @selector(onInvoiceStyleLoadFail:); [_mDownManager postHttpRequest:ServerURL dic:dict path:nil fileName:nil]; } -(void)onInvoiceStyleLoadFinish:(ASIDownManager*)sender { [self cancel]; RequestResultModel *resultModel = [RequestResultModel dk_modelWithJSON:sender.mWebStr]; int iStatus = resultModel.status; NSString *message=resultModel.message; if(iStatus==0) { NSDictionary *resultDic= (NSDictionary*)resultModel.result; NSArray *resultArray= [resultDic objectForKey:@"Table"]; if(resultArray!=nil&&resultArray.count>0) { _arrSearch = [[NSMutableArray alloc]initWithArray:resultArray]; _arrFilter = [[NSMutableArray alloc] initWithArray:_arrSearch]; if(_arrSearch.count == 0){ [self showAlertViewText:@"未找到匹配结果"]; } [self.tableView reloadData]; } } else if(iStatus == ActionResultStatusAuthError ||iStatus == ActionResultStatusNoLogin ||iStatus == ActionResultStatusLogined||iStatus==ActionResultSessionOverdue){ [self showReLoginDialog:message]; return; }else { [self.tableView reloadData]; [self showAlertViewText:message]; } } -(void)onInvoiceStyleLoadFail:(ASIDownManager *)sender { [self cancel]; [self showAlertViewText:@"网络异常"]; } /** 取消进度条 */ - (void)cancel { [self stopLoading]; } /** tableview的分区数 @param tableView tableView description @return return value description */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } /** tableview每个分区的行数 @param tableView <#tableView description#> @param section <#section description#> @return <#return value description#> */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _arrFilter.count; } /** 获取tableviewcell @param tableView <#tableView description#> @param indexPath <#indexPath description#> @return <#return value description#> */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier ]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } NSDictionary *dic=_arrFilter[indexPath.row]; NSString *name = [dic objectForKey:@"InvoiceLayoutName"]; cell.textLabel.textAlignment = NSTextAlignmentCenter; cell.textLabel.font=textFont; cell.textLabel.text=name; return cell; } /** 增加取消按钮 */ - (void)addCancelButton { UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; [button2 addTarget:self action:@selector(cancelSearch) forControlEvents:UIControlEventTouchUpInside]; button2.frame = CGRectMake(0, 0, 40, 24); [button2 setTitle:@"取消" forState:UIControlStateNormal]; [button2 setTitleColor:NavBarItemColor forState:UIControlStateNormal]; UIBarButtonItem *menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:button2]; self.navigationItem.rightBarButtonItem = menuButton2; } /** 取消查询按钮 */ - (void)cancelSearch { [_searchBar resignFirstResponder]; [self removeCancel]; } /** 移除取消按钮 */ - (void) removeCancel { self.navigationItem.rightBarButtonItem = nil; } /** 点击tableview的每一行 @param tableView <#tableView description#> @param indexPath <#indexPath description#> */ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dic=_arrFilter[indexPath.row]; NSString *styleName = [dic objectForKey:@"InvoiceLayoutName"]; NSString *styleId = [dic objectForKey:@"InvoiceLayoutID"]; if([self.inventoryDelegate respondsToSelector:@selector(getInvoiceStyle:name:)]){ [self.inventoryDelegate getInvoiceStyle:styleId name:styleName]; } [self.navigationController popViewControllerAnimated:YES]; } /** 搜索栏开始编辑 @param searchBar <#searchBar description#> @return <#return value description#> */ - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { [self addCancelButton]; return YES; } /** searchbar委托函数 @param searchBar <#searchBar description#> @param range <#range description#> @param text <#text description#> @return <#return value description#> */ - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { return YES; } /** searchBar文本变化完成 @param searchBar <#searchBar description#> @param searchText <#searchText description#> */ - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if ([searchText isEqualToString:@""]) { self.arrFilter = _arrSearch; [_tableView reloadData]; return; } NSString *keyName = @""; keyName = @"InvoiceLayoutName"; NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"InvoiceLayoutName",searchText]; NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_arrSearch filteredArrayUsingPredicate:predicateString]]; self.arrFilter = filteredArray; [_tableView reloadData]; } /** 取消按钮点击函数 @param searchBar <#searchBar description#> */ - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; [self cancelSearch]; } /** scrollview回调事件 @param scrollView <#scrollView description#> */ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [_searchBar resignFirstResponder]; [self cancelSearch]; } @end