WareSearchViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // WareSearchViewController.m
  3. // IBOSSmini
  4. //
  5. // Created by apple on 2017/5/23.
  6. // Copyright © 2017年 elongtian. All rights reserved.
  7. //
  8. #import "WareSearchViewController.h"
  9. #import "BaseIDAndNameModel.h"
  10. @interface WareSearchViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
  11. @property(nonatomic,strong) ASIDownManager *downManager;
  12. @property(nonatomic,strong) UISearchBar *searchBar;
  13. @property(nonatomic,strong) UITableView *tableView;
  14. @end
  15. @implementation WareSearchViewController
  16. #pragma mark - 公共函数
  17. /**
  18. viewDidLoad
  19. */
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.navigationItem.title=@"库区列表";
  23. //是否是dismissViewController
  24. if (_isPresentViewFlg) {
  25. UIView *vTitle = [[UIView alloc]init];
  26. vTitle.frame = CGRectMake(0, 0, Screen_Width, 44 + rectStatusHeight);
  27. vTitle.backgroundColor = NavigationBarTintColor;
  28. [self.view addSubview:vTitle];
  29. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  30. [button setImage:[UIImage imageNamed:@"icon_back"]
  31. forState:UIControlStateNormal];
  32. [button addTarget:self action:@selector(GotoBack)
  33. forControlEvents:UIControlEventTouchUpInside];
  34. button.frame = CGRectMake(20, rectStatusHeight + 13, 9, 15);
  35. [vTitle addSubview:button];
  36. UILabel *lblTitle = [[UILabel alloc]init];
  37. lblTitle.frame = CGRectMake(9, rectStatusHeight + 13, Screen_Width-2*9 - 20, 15);
  38. lblTitle.textColor = [UIColor whiteColor];
  39. lblTitle.text = @"请选择";
  40. lblTitle.textAlignment = NSTextAlignmentCenter;
  41. [vTitle addSubview:lblTitle];
  42. }else{
  43. self.navigationItem.title=@"请选择";
  44. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  45. [button setImage:[UIImage imageNamed:@"icon_back"]
  46. forState:UIControlStateNormal];
  47. [button addTarget:self action:@selector(GotoBack)
  48. forControlEvents:UIControlEventTouchUpInside];
  49. button.frame = CGRectMake(0, 0, 15, 18);
  50. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  51. self.navigationItem.leftBarButtonItem = menuButton;
  52. [self.navigationController setNavigationBarHidden:NO];
  53. }
  54. _tableView=[UITableView new];
  55. _tableView.delegate=self;
  56. _tableView.dataSource=self;
  57. //是否是dismissViewController
  58. if (_isPresentViewFlg) {
  59. self.tableView.frame = CGRectMake(0, 44 + rectStatusHeight, self.view.bounds.size.width, self.view.bounds.size.height - 44 - rectStatusHeight - 1) ;
  60. }
  61. else{
  62. self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-rectNavHeight - rectStatusHeight - 1) ;
  63. }
  64. [self.view addSubview:_tableView];
  65. //搜索框
  66. _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
  67. _searchBar.delegate=self;
  68. self.tableView.tableHeaderView=_searchBar;
  69. }
  70. /**
  71. didReceiveMemoryWarning
  72. */
  73. - (void)didReceiveMemoryWarning {
  74. [super didReceiveMemoryWarning];
  75. }
  76. /**
  77. viewWillAppear
  78. @param animated <#animated description#>
  79. */
  80. - (void)viewWillAppear:(BOOL)animated
  81. {
  82. _searchArr=[NSMutableArray new];
  83. _filterArr=[NSMutableArray new];
  84. if(_jsonArr==nil){
  85. _downManager = [[ASIDownManager alloc] init];
  86. _downManager.delegate = self;
  87. _downManager.OnImageDown = @selector(onLoadFinish:);
  88. _downManager.OnImageFail = @selector(onLoadFail:);
  89. [self reloadData];
  90. }
  91. else{
  92. _searchArr=[[NSMutableArray alloc]initWithArray:_jsonArr];
  93. _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr];
  94. [_tableView reloadData];
  95. }
  96. }
  97. /**
  98. 修改:2017-9-25
  99. 适配机型
  100. 安全区视图发生变化
  101. */
  102. -(void)viewSafeAreaInsetsDidChange{
  103. //是否是dismissViewController
  104. if (_isPresentViewFlg) {
  105. self.tableView.frame = CGRectMake(0, 44+ rectStatusHeight, self.view.bounds.size.width, self.view.safeAreaLayoutGuide.layoutFrame.size.height - 44) ;
  106. }
  107. else{
  108. self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.safeAreaLayoutGuide.layoutFrame.size.height) ;
  109. }
  110. [super viewSafeAreaInsetsDidChange];
  111. }
  112. /**
  113. dealloc
  114. */
  115. - (void)dealloc
  116. {
  117. [self Cancel];
  118. }
  119. #pragma mark - 委托回调函数
  120. #pragma mark - tableView回调
  121. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  122. {
  123. return 1;
  124. }
  125. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  126. {
  127. return _filterArr.count;
  128. }
  129. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. static NSString *CellIdentifier = @"Cell";
  132. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
  133. if(cell==nil)
  134. {
  135. cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  136. }
  137. NSDictionary *dic=_filterArr[indexPath.row];
  138. cell.textLabel.text=[dic objectForKey:@"WarehouseName"];
  139. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  140. cell.textLabel.font = [UIFont systemFontOfSize:TextFontOfSize];
  141. return cell;
  142. }
  143. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  144. NSDictionary *dic=_filterArr[indexPath.row];
  145. BaseIDAndNameModel *model = [BaseIDAndNameModel new ];
  146. model.id = [dic objectForKey:@"WarehouseCode"];
  147. model.warehouseId=[NSString stringWithFormat:@"%d",[[dic objectForKey:@"WarehouseID"]integerValue]];
  148. model.name = [dic objectForKey:@"WarehouseName"];
  149. if ([self.bDelegate respondsToSelector:@selector(baseIDAndNameDoneDatas : BaseIDAndName:)]){
  150. [self.bDelegate baseIDAndNameDoneDatas:model BaseIDAndName:self.showDialogViewTag];
  151. }
  152. //是否是dismissViewController
  153. if (_isPresentViewFlg) {
  154. [self dismissViewControllerAnimated:YES completion:nil];
  155. }else{
  156. [self.navigationController popViewControllerAnimated:YES];
  157. }
  158. }
  159. #pragma mark - 数据回调
  160. /**
  161. 数据加载成功回调
  162. @param sender <#sender description#>
  163. */
  164. - (void)onLoadFinish:(ASIDownManager *)sender{
  165. // 服务器返回数据
  166. NSDictionary *dic = [sender.mWebStr JSONValue];
  167. [self Cancel];
  168. // 服务器返回数据是否正确
  169. if (dic && [dic isKindOfClass:[NSDictionary class]])
  170. {
  171. // 服务器返回数据状态值
  172. int iStatus = [[dic objectForKey:@"Status"] intValue];
  173. // 服务器返回数据消息
  174. NSString *message=[dic objectForKey:@"Message"];
  175. // 服务器返回数据状态值正确
  176. if (iStatus == 0)
  177. {
  178. NSArray * approvArr=[dic objectForKey:@"Result"];
  179. if(approvArr!=nil)
  180. {
  181. _searchArr=[[NSMutableArray alloc]initWithArray:approvArr];
  182. _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr];
  183. if(_searchArr.count==0){
  184. [self showAlertViewText:@"未找到匹配结果"];
  185. }
  186. [self.tableView reloadData];
  187. }
  188. }
  189. // 服务器返回数据状态值异常
  190. else if(iStatus==ActionResultStatusAuthError
  191. ||iStatus==ActionResultStatusNoLogin
  192. ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){
  193. [self showReLoginDialog:message];
  194. }
  195. else {
  196. [self.tableView reloadData];
  197. [self showAlertViewText:message];
  198. }
  199. }
  200. }
  201. /**
  202. 加载失败
  203. @param sender <#sender description#>
  204. */
  205. - (void)onLoadFail:(ASIDownManager *)sender {
  206. [self Cancel];
  207. [self.tableView reloadData];
  208. [self showAlertViewText:@"加载失败"];
  209. }
  210. #pragma mark searchbar回调
  211. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
  212. {
  213. [self AddCancelButton];
  214. return YES;
  215. }
  216. - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  217. {
  218. return YES;
  219. }
  220. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  221. {
  222. if ([searchText isEqualToString:@""]) {
  223. self.filterArr = _searchArr;
  224. [_tableView reloadData];
  225. return;
  226. }
  227. NSString *keyName = @"WarehouseName";
  228. //< 模糊查找
  229. NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", keyName, searchText];
  230. //< 精确查找
  231. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText];
  232. NSLog(@"predicate %@",predicateString);
  233. NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]];
  234. self.filterArr = filteredArray;
  235. [_tableView reloadData];
  236. }
  237. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  238. {
  239. [searchBar resignFirstResponder];
  240. [self CancelSearch];
  241. }
  242. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  243. {
  244. [_searchBar resignFirstResponder];
  245. [self CancelSearch];
  246. }
  247. #pragma mark - 私有函数
  248. /**
  249. 进度条隐藏
  250. */
  251. - (void)Cancel {
  252. [self stopLoading];
  253. }
  254. /**
  255. 添加取消按钮
  256. */
  257. - (void)AddCancelButton
  258. {
  259. //添加取消按钮
  260. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  261. [button addTarget:self action:@selector(CancelSearch)
  262. forControlEvents:UIControlEventTouchUpInside];
  263. button.frame = CGRectMake(0, 0, 40, 24);
  264. [button setTitle:@"取消" forState:UIControlStateNormal];
  265. [button setTitleColor:NavBarItemColor forState:UIControlStateNormal];
  266. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  267. self.navigationItem.rightBarButtonItem = menuButton;
  268. }
  269. /**
  270. 取消查询
  271. */
  272. - (void)CancelSearch
  273. {
  274. [_searchBar resignFirstResponder];
  275. [self RemoveCancel];
  276. }
  277. /**
  278. 取消按钮隐藏
  279. */
  280. - (void) RemoveCancel
  281. {
  282. self.navigationItem.rightBarButtonItem = nil;
  283. }
  284. /**
  285. 加载数据
  286. */
  287. - (void)reloadData
  288. {
  289. [self startLoading];
  290. NSString *urlStr = ServerURL;
  291. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  292. [dict setObject:@"GetDataSource" forKey:@"Action"];
  293. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  294. [dict setObject:kkUserCode forKey:@"UserCode"];
  295. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  296. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  297. [dict setObject:@"T_MST_Warehouse" forKey:@"DataSourceCode"];
  298. [_downManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];
  299. }
  300. /**
  301. 返回父界面
  302. */
  303. - (void)GotoBack
  304. {
  305. //是否是dismissViewController
  306. if (_isPresentViewFlg) {
  307. [self dismissViewControllerAnimated:YES completion:nil];
  308. }else{
  309. [self.navigationController popViewControllerAnimated:YES];
  310. }
  311. }
  312. @end