BrandSearchViewController.m 9.8 KB

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