BrandSearchViewController.m 11 KB

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