KeHuXingZhiSearchTC.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // KeHuXingZhiSearchTC.m
  3. // IBOSSIPAD
  4. //
  5. // Created by iHope on 14-8-14.
  6. // Copyright (c) 2014年 elongtian. All rights reserved.
  7. //
  8. #import "KeHuXingZhiSearchTC.h"
  9. @interface KeHuXingZhiSearchTC()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
  10. @property(nonatomic,strong) ASIDownManager *mDownManager;
  11. @property(strong) IBOutlet UISearchBar *searchBar;
  12. @property(strong) UITableView *tableView;
  13. @end
  14. @implementation KeHuXingZhiSearchTC
  15. - (void)OnLoadFinish:(ASIDownManager *)sender {
  16. NSDictionary *dic = [sender.mWebStr JSONValue];
  17. NSLog(@"dic=%@",dic);
  18. [self Cancel];
  19. if (dic && [dic isKindOfClass:[NSDictionary class]]) {
  20. int iStatus = [[dic objectForKey:@"Status"] intValue];
  21. NSString *message=[dic objectForKey:@"Message"];
  22. if (iStatus == 0) {
  23. NSArray * approvArr=[dic objectForKey:@"Result"];
  24. if(approvArr!=nil)
  25. {
  26. _searchArr=[[NSMutableArray alloc]initWithArray:approvArr];
  27. _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr];
  28. if(_searchArr.count==0)
  29. {
  30. [self showAlertViewText:@"未找到匹配结果"];
  31. }
  32. [self.tableView reloadData];
  33. }
  34. }
  35. else if(iStatus==-2||iStatus==-3||iStatus==-4){
  36. __weak id weakapp=Appdelegate;
  37. __weak id userShare=[UserInfoManager Share] ;
  38. [UIAlertView showAlertViewWithTitle:@""
  39. message:@"加载失败,请您重新登录"
  40. cancelButtonTitle:nil
  41. otherButtonTitles:[NSArray arrayWithObject:@"OK"]
  42. onDismiss:^(int buttonIndex) {
  43. [userShare ClearUserData];
  44. [weakapp changeViewDidLogout];
  45. }
  46. onCancel:^ {}];
  47. }
  48. else {
  49. [self.tableView reloadData];
  50. [self showAlertViewText:message];
  51. }
  52. }
  53. }
  54. - (void)OnLoadFail:(ASIDownManager *)sender {
  55. [self Cancel];
  56. [self.tableView reloadData];
  57. [self showAlertViewText:@"加载失败"];
  58. }
  59. - (void)Cancel {
  60. [self StopLoading];
  61. }
  62. -(void)reloadData
  63. {
  64. [self StartLoading];
  65. NSString *urlStr = ServerURL;
  66. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  67. [dict setObject:@"GetDataSource" forKey:@"Action"];
  68. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  69. [dict setObject:kkUserCode forKey:@"UserCode"];
  70. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  71. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  72. [dict setObject:@"CustomerType" forKey:@"DataSourceCode"];
  73. [_mDownManager PostHttpRequest:urlStr :dict :nil :nil];
  74. }
  75. -(void)GotoBack
  76. {
  77. if(!_Subview){
  78. [self dismissViewControllerAnimated:YES completion:^(void){
  79. }];}
  80. else{
  81. [self.navigationController popViewControllerAnimated:YES];
  82. }
  83. }
  84. -(void)viewWillAppear:(BOOL)animated
  85. {
  86. _searchArr=[NSMutableArray new];
  87. _filterArr=[NSMutableArray new];
  88. self.mDownManager = [[ASIDownManager alloc] init];
  89. self.mDownManager = [[ASIDownManager alloc] init];
  90. _mDownManager.delegate = self;
  91. _mDownManager.OnImageDown = @selector(OnLoadFinish:);
  92. _mDownManager.OnImageFail = @selector(OnLoadFail:);
  93. [self reloadData];
  94. }
  95. - (void)viewDidLoad
  96. {
  97. [super viewDidLoad];
  98. self.navigationItem.title=@"客户性质";
  99. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  100. [button setBackgroundImage:[UIImage imageNamed:@"fanhui_icon.png"]
  101. forState:UIControlStateNormal];
  102. [button addTarget:self action:@selector(GotoBack)
  103. forControlEvents:UIControlEventTouchUpInside];
  104. button.frame = CGRectMake(0, 0, 44, 20);
  105. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  106. self.navigationItem.leftBarButtonItem = menuButton;
  107. _tableView=[UITableView new];
  108. _tableView.delegate=self;
  109. _tableView.dataSource=self;
  110. _tableView.frame=CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height) ;
  111. [self.view addSubview:_tableView];
  112. //搜索框
  113. _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 44)];
  114. _searchBar.delegate=self;
  115. self.tableView.tableHeaderView=_searchBar;
  116. }
  117. -(void)dealloc
  118. {
  119. [self Cancel];
  120. }
  121. - (void)didReceiveMemoryWarning
  122. {
  123. [super didReceiveMemoryWarning];
  124. // Dispose of any resources that can be recreated.
  125. }
  126. #pragma mark - Table view data source
  127. /*- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. return 135.0f;
  130. }*/
  131. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  132. {
  133. // Return the number of sections.
  134. return 1;
  135. }
  136. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  137. {
  138. // Return the number of rows in the section.
  139. return _filterArr.count;
  140. }
  141. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. static NSString *CellIdentifier = @"Cell";
  144. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
  145. if(cell==nil)
  146. {
  147. cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  148. }
  149. NSDictionary *dic=_filterArr[indexPath.row];
  150. NSString *name=[dic objectForKey:@"CustomerTypeName"];
  151. cell.textLabel.text=name;
  152. return cell;
  153. }
  154. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  155. if(!_Subview){
  156. [self dismissViewControllerAnimated:YES completion:^(void){
  157. NSDictionary *dic=_filterArr[indexPath.row];
  158. NSString * str=[dic objectForKey:@"CustomerTypeName"];
  159. NSString *WareID=[dic objectForKey:@"CustomerType"];
  160. [_parentVC.btnKeHuXingZhi setTitle:str forState:UIControlStateNormal];
  161. _parentVC.KeHuXingZhiHiddenStr=WareID;
  162. }];
  163. }
  164. else {
  165. NSDictionary *dic=_filterArr[indexPath.row];
  166. NSString * str=[dic objectForKey:@"CustomerTypeName"];
  167. NSString *WareID=[dic objectForKey:@"CustomerType"];
  168. [_parentVC2.btnKeHuXingZhi setTitle:str forState:UIControlStateNormal];
  169. _parentVC2.KeHuXingZhiHiddenStr=WareID;
  170. [self.navigationController popViewControllerAnimated:YES];
  171. }
  172. // [self.navigationController popViewControllerAnimated:YES];
  173. }
  174. -(void)AddCancelButton
  175. {
  176. //添加取消按钮
  177. UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
  178. [button2 addTarget:self action:@selector(CancelSearch)
  179. forControlEvents:UIControlEventTouchUpInside];
  180. button2.frame = CGRectMake(0, 0, 40, 24);
  181. [button2 setTitle:@"取消" forState:UIControlStateNormal];
  182. [button2 setTitleColor:NavBarItemColor forState:UIControlStateNormal];
  183. UIBarButtonItem *menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
  184. self.navigationItem.rightBarButtonItem = menuButton2;
  185. }
  186. -(void)CancelSearch
  187. {
  188. [_searchBar resignFirstResponder];
  189. [self RemoveCancel];
  190. }
  191. -(void) RemoveCancel
  192. {
  193. self.navigationItem.rightBarButtonItem = nil;
  194. }
  195. #pragma mark search bar delegate
  196. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
  197. {
  198. [self AddCancelButton];
  199. return YES;
  200. }
  201. - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  202. {
  203. return YES;
  204. }
  205. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  206. {
  207. if ([searchText isEqualToString:@""]) {
  208. self.filterArr = _searchArr;
  209. [_tableView reloadData];
  210. return;
  211. }
  212. NSString *keyName = @"";
  213. // if (_searchState == DataSearchStateBank) {
  214. keyName = @"CustomerTypeName";
  215. //}
  216. //< 模糊查找
  217. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"OrganizationCode",searchText];
  218. NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"CustomerTypeName",searchText];
  219. //< 精确查找
  220. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText];
  221. NSLog(@"predicate %@",predicateString);
  222. NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]];
  223. self.filterArr = filteredArray;
  224. [_tableView reloadData];
  225. }
  226. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  227. {
  228. [searchBar resignFirstResponder];
  229. [self CancelSearch];
  230. }
  231. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  232. {
  233. [_searchBar resignFirstResponder];
  234. [self CancelSearch];
  235. }
  236. @end