// // WareSearchViewController.m // IBOSSmini // // Created by apple on 2017/5/23. // Copyright © 2017年 elongtian. All rights reserved. // #import "WareSearchViewController.h" #import "BaseIDAndNameModel.h" @interface WareSearchViewController () @property(nonatomic,strong) ASIDownManager *downManager; @property(nonatomic,strong) UISearchBar *searchBar; @property(nonatomic,strong) UITableView *tableView; @end @implementation WareSearchViewController #pragma mark - 公共函数 /** viewDidLoad */ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title=@"库区列表"; //是否是dismissViewController if (_isPresentViewFlg) { UIView *vTitle = [[UIView alloc]init]; vTitle.frame = CGRectMake(0, 0, Screen_Width, 44 + rectStatusHeight); vTitle.backgroundColor = NavigationBarTintColor; [self.view addSubview:vTitle]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(GotoBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(20, rectStatusHeight + 13, 9, 15); [vTitle addSubview:button]; UILabel *lblTitle = [[UILabel alloc]init]; lblTitle.frame = CGRectMake(9, rectStatusHeight + 13, Screen_Width-2*9 - 20, 15); lblTitle.textColor = [UIColor whiteColor]; lblTitle.text = @"请选择"; lblTitle.textAlignment = NSTextAlignmentCenter; [vTitle addSubview:lblTitle]; }else{ self.navigationItem.title=@"请选择"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(GotoBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 15, 18); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; [self.navigationController setNavigationBarHidden:NO]; } _tableView=[UITableView new]; _tableView.delegate=self; _tableView.dataSource=self; //是否是dismissViewController if (_isPresentViewFlg) { self.tableView.frame = CGRectMake(0, 44 + rectStatusHeight, self.view.bounds.size.width, self.view.bounds.size.height - 44 - rectStatusHeight - 1) ; } else{ self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-rectNavHeight - rectStatusHeight - 1) ; } [self.view addSubview:_tableView]; //搜索框 _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; _searchBar.delegate=self; self.tableView.tableHeaderView=_searchBar; } /** didReceiveMemoryWarning */ - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } /** viewWillAppear @param animated <#animated description#> */ - (void)viewWillAppear:(BOOL)animated { _searchArr=[NSMutableArray new]; _filterArr=[NSMutableArray new]; if(_jsonArr==nil){ _downManager = [[ASIDownManager alloc] init]; _downManager.delegate = self; _downManager.OnImageDown = @selector(onLoadFinish:); _downManager.OnImageFail = @selector(onLoadFail:); [self reloadData]; } else{ _searchArr=[[NSMutableArray alloc]initWithArray:_jsonArr]; _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr]; [_tableView reloadData]; } } /** 修改:2017-9-25 适配机型 安全区视图发生变化 */ -(void)viewSafeAreaInsetsDidChange{ //是否是dismissViewController if (_isPresentViewFlg) { self.tableView.frame = CGRectMake(0, 44+ rectStatusHeight, self.view.bounds.size.width, self.view.safeAreaLayoutGuide.layoutFrame.size.height - 44) ; } else{ self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.safeAreaLayoutGuide.layoutFrame.size.height) ; } [super viewSafeAreaInsetsDidChange]; } /** dealloc */ - (void)dealloc { [self Cancel]; } #pragma mark - 委托回调函数 #pragma mark - tableView回调 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _filterArr.count; } - (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=_filterArr[indexPath.row]; cell.textLabel.text=[dic objectForKey:@"WarehouseName"]; cell.textLabel.textAlignment = NSTextAlignmentCenter; cell.textLabel.font = [UIFont systemFontOfSize:TextFontOfSize]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dic=_filterArr[indexPath.row]; BaseIDAndNameModel *model = [BaseIDAndNameModel new ]; model.id = [dic objectForKey:@"WarehouseCode"]; model.warehouseId=[NSString stringWithFormat:@"%d",[[dic objectForKey:@"WarehouseID"]integerValue]]; model.name = [dic objectForKey:@"WarehouseName"]; if ([self.bDelegate respondsToSelector:@selector(baseIDAndNameDoneDatas : BaseIDAndName:)]){ [self.bDelegate baseIDAndNameDoneDatas:model BaseIDAndName:self.showDialogViewTag]; } //是否是dismissViewController if (_isPresentViewFlg) { [self dismissViewControllerAnimated:YES completion:nil]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark - 数据回调 /** 数据加载成功回调 @param sender <#sender description#> */ - (void)onLoadFinish:(ASIDownManager *)sender{ // 服务器返回数据 NSDictionary *dic = [sender.mWebStr JSONValue]; [self Cancel]; // 服务器返回数据是否正确 if (dic && [dic isKindOfClass:[NSDictionary class]]) { // 服务器返回数据状态值 int iStatus = [[dic objectForKey:@"Status"] intValue]; // 服务器返回数据消息 NSString *message=[dic objectForKey:@"Message"]; // 服务器返回数据状态值正确 if (iStatus == 0) { NSArray * approvArr=[dic objectForKey:@"Result"]; if(approvArr!=nil) { _searchArr=[[NSMutableArray alloc]initWithArray:approvArr]; _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr]; if(_searchArr.count==0){ [self showAlertViewText:@"未找到匹配结果"]; } [self.tableView reloadData]; } } // 服务器返回数据状态值异常 else if(iStatus==ActionResultStatusAuthError ||iStatus==ActionResultStatusNoLogin ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){ [self showReLoginDialog:message]; } else { [self.tableView reloadData]; [self showAlertViewText:message]; } } } /** 加载失败 @param sender <#sender description#> */ - (void)onLoadFail:(ASIDownManager *)sender { [self Cancel]; [self.tableView reloadData]; [self showAlertViewText:@"加载失败"]; } #pragma mark searchbar回调 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { [self AddCancelButton]; return YES; } - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { return YES; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if ([searchText isEqualToString:@""]) { self.filterArr = _searchArr; [_tableView reloadData]; return; } NSString *keyName = @"WarehouseName"; //< 模糊查找 NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", keyName, searchText]; //< 精确查找 // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText]; NSLog(@"predicate %@",predicateString); NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]]; self.filterArr = filteredArray; [_tableView reloadData]; } - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; [self CancelSearch]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [_searchBar resignFirstResponder]; [self CancelSearch]; } #pragma mark - 私有函数 /** 进度条隐藏 */ - (void)Cancel { [self stopLoading]; } /** 添加取消按钮 */ - (void)AddCancelButton { //添加取消按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(CancelSearch) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 40, 24); [button setTitle:@"取消" forState:UIControlStateNormal]; [button setTitleColor:NavBarItemColor forState:UIControlStateNormal]; UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.rightBarButtonItem = menuButton; } /** 取消查询 */ - (void)CancelSearch { [_searchBar resignFirstResponder]; [self RemoveCancel]; } /** 取消按钮隐藏 */ - (void) RemoveCancel { self.navigationItem.rightBarButtonItem = nil; } /** 加载数据 */ - (void)reloadData { [self startLoading]; NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"GetDataSource" forKey:@"Action"]; [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [dict setObject:@"T_MST_Warehouse" forKey:@"DataSourceCode"]; [_downManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; } /** 返回父界面 */ - (void)GotoBack { //是否是dismissViewController if (_isPresentViewFlg) { [self dismissViewControllerAnimated:YES completion:nil]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } @end