WareSearchViewController.m 11 KB

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