SalesManSearchVC.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // IBOSSIPAD
  3. //
  4. // Created by iHope on 14-8-14.
  5. // Copyright (c) 2014年 elongtian. All rights reserved.
  6. //
  7. #import "SalesManSearchVC.h"
  8. #import "SalesManSearchCell.h"
  9. @interface SalesManSearchVC()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
  10. {
  11. UITextField *txtCode;
  12. UITextField *txtName;
  13. }
  14. @property(nonatomic,strong) ASIDownManager *mDownManager;
  15. @property(nonatomic,strong) UITableView *tableView;
  16. @end
  17. @implementation SalesManSearchVC
  18. #pragma mark - 公共函数
  19. /**
  20. viewDidLoad函数
  21. */
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. _searchArr=[NSMutableArray new];
  26. _filterArr=[NSMutableArray new];
  27. self.navigationItem.title=@"业务员查询";
  28. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  29. [button setBackgroundImage:[UIImage imageNamed:@"icon_back"]
  30. forState:UIControlStateNormal];
  31. [button addTarget:self action:@selector(goBack)
  32. forControlEvents:UIControlEventTouchUpInside];
  33. button.frame = CGRectMake(0, 0, 15, 18);
  34. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  35. self.navigationItem.leftBarButtonItem = menuButton;
  36. self.navigationController.navigationBar.barTintColor = NavigationBarTintColor;
  37. self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:16]};
  38. _tableView=[UITableView new];
  39. _tableView.delegate=self;
  40. _tableView.dataSource=self;
  41. _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  42. _tableView.frame=CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height) ;
  43. [self.view addSubview:_tableView];
  44. _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
  45. _searchBar.delegate=self;
  46. self.tableView.tableHeaderView=_searchBar;
  47. [self reloadData];
  48. }
  49. #pragma mark-委托函数
  50. /**
  51. tableviewcell的高度
  52. @param tableView <#tableView description#>
  53. @param indexPath <#indexPath description#>
  54. @return <#return value description#>
  55. */
  56. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  57. {
  58. return 89.0f;
  59. }
  60. /**
  61. tableview的分区数
  62. @param tableView <#tableView description#>
  63. @return <#return value description#>
  64. */
  65. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  66. {
  67. return 1;
  68. }
  69. /**
  70. 获取tableview的行数
  71. @param tableView <#tableView description#>
  72. @param section <#section description#>
  73. @return <#return value description#>
  74. */
  75. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  76. {
  77. return _filterArr.count;
  78. }
  79. /**
  80. 获取tableviewcell
  81. @param tableView <#tableView description#>
  82. @param indexPath <#indexPath description#>
  83. @return <#return value description#>
  84. */
  85. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. static NSString *CellIdentifier = @"Cell";
  88. SalesManSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
  89. if(cell==nil)
  90. {
  91. NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"SalesManSearchCell" owner:self options:nil];
  92. cell = [nib objectAtIndex:0];
  93. }
  94. NSDictionary *dic=_filterArr[indexPath.row];
  95. NSString *name=[dic objectForKey:@"StaffName"];
  96. NSString *code=[dic objectForKey:@"StaffCode"];
  97. NSString *fullname=[dic objectForKey:@"OrganizationName"];
  98. cell.lblStaffName.text=name;
  99. cell.lblStaffCode.text=code;
  100. cell.lblDepartment.text=fullname;
  101. cell.lblStaffId=[dic objectForKey:@"StaffID"];
  102. return cell;
  103. }
  104. /**
  105. tableviewcell的点击事件
  106. @param tableView <#tableView description#>
  107. @param indexPath <#indexPath description#>
  108. */
  109. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  110. [self dismissViewControllerAnimated:YES completion:^(void){
  111. NSDictionary *dic=_filterArr[indexPath.row];
  112. NSString *staffName=[dic objectForKey:@"StaffName"];
  113. NSString *staffId=[dic objectForKey:@"StaffID"];
  114. if([self.salesmanDelegate respondsToSelector:@selector(salesman:salesmanName:)]){
  115. [self.salesmanDelegate salesman:staffId salesmanName:staffName];
  116. }
  117. }];
  118. }
  119. /**
  120. 数据加载成功函数
  121. @param sender <#sender description#>
  122. */
  123. - (void)onLoadFinish:(ASIDownManager *)sender {
  124. NSDictionary *dic = [sender.mWebStr JSONValue];
  125. [self cancel];
  126. if (dic && [dic isKindOfClass:[NSDictionary class]]) {
  127. int iStatus = [[dic objectForKey:@"Status"] intValue];
  128. NSString *message=[dic objectForKey:@"Message"];
  129. if (iStatus == 0) {
  130. NSArray * approvArr=[dic objectForKey:@"Result"];
  131. if(approvArr!=nil)
  132. {
  133. _searchArr=[[NSMutableArray alloc]initWithArray:approvArr];
  134. _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr];
  135. if(_searchArr.count==0)
  136. {
  137. [self showAlertViewText:@"未找到匹配结果"];
  138. }
  139. [self.tableView reloadData];
  140. }
  141. }
  142. else if(iStatus==ActionResultStatusAuthError
  143. ||iStatus==ActionResultStatusNoLogin
  144. ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){
  145. [self showReLoginDialog:message];
  146. return;
  147. }
  148. else {
  149. [self.tableView reloadData];
  150. [self showAlertViewText:message];
  151. }
  152. }
  153. }
  154. /**
  155. 数据加载失败函数
  156. @param sender <#sender description#>
  157. */
  158. - (void)onLoadFail:(ASIDownManager *)sender {
  159. [self cancel];
  160. [self.tableView reloadData];
  161. [self showAlertViewText:@"加载失败"];
  162. }
  163. /**
  164. 搜索栏开始编辑函数
  165. @param searchBar <#searchBar description#>
  166. @return <#return value description#>
  167. */
  168. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
  169. {
  170. [self addCancelButton];
  171. return YES;
  172. }
  173. /**
  174. 是否允许替换文本
  175. @param searchBar <#searchBar description#>
  176. @param range <#range description#>
  177. @param text <#text description#>
  178. @return <#return value description#>
  179. */
  180. - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  181. {
  182. return YES;
  183. }
  184. /**
  185. 搜索栏文本输入结束回调函数
  186. @param searchBar <#searchBar description#>
  187. @param searchText <#searchText description#>
  188. */
  189. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  190. {
  191. if ([searchText isEqualToString:@""]) {
  192. self.filterArr = _searchArr;
  193. [_tableView reloadData];
  194. return;
  195. }
  196. NSString *keyName = @"";
  197. // if (_searchState == DataSearchStateBank) {
  198. keyName = @"StaffName";
  199. //}
  200. //< 模糊查找
  201. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"OrganizationCode",searchText];
  202. NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"StaffName",searchText];
  203. //< 精确查找
  204. // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText];
  205. NSLog(@"predicate %@",predicateString);
  206. NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]];
  207. self.filterArr = filteredArray;
  208. [_tableView reloadData];
  209. }
  210. /**
  211. 取消搜索函数
  212. @param searchBar <#searchBar description#>
  213. */
  214. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  215. {
  216. [searchBar resignFirstResponder];
  217. [self cancelSearch];
  218. }
  219. /**
  220. scrollView回调函数
  221. @param scrollView <#scrollView description#>
  222. */
  223. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  224. {
  225. [_searchBar resignFirstResponder];
  226. [self cancelSearch];
  227. }
  228. #pragma mark - 私有函数
  229. /**
  230. 返回函数
  231. */
  232. -(void)goBack
  233. {
  234. if(!_subview){
  235. [self dismissViewControllerAnimated:YES completion:^(void){
  236. }];}
  237. else{
  238. [self.navigationController popViewControllerAnimated:YES];
  239. }
  240. }
  241. /**
  242. 取消进度条
  243. */
  244. - (void)cancel {
  245. [self stopLoading];
  246. }
  247. /**
  248. 加载数据接口
  249. */
  250. -(void)reloadData
  251. {
  252. [self startLoading];
  253. self.mDownManager = [[ASIDownManager alloc] init];
  254. _mDownManager.delegate = self;
  255. _mDownManager.OnImageDown = @selector(onLoadFinish:);
  256. _mDownManager.OnImageFail = @selector(onLoadFail:);
  257. NSString *urlStr = ServerURL;
  258. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  259. [dict setObject:@"GetStaffDataSource" forKey:@"Action"];
  260. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  261. [dict setObject:kkUserCode forKey:@"UserCode"];
  262. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  263. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  264. [dict setObject:@"" forKey:@"StaffCode"];
  265. [dict setObject:@"" forKey:@"StaffName"];
  266. [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];}
  267. /**
  268. 添加取消按钮
  269. */
  270. -(void)addCancelButton
  271. {
  272. UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
  273. [button2 addTarget:self action:@selector(cancelSearch)
  274. forControlEvents:UIControlEventTouchUpInside];
  275. button2.frame = CGRectMake(0, 0, 40, 24);
  276. [button2 setTitle:@"取消" forState:UIControlStateNormal];
  277. [button2 setTitleColor:NavBarItemColor forState:UIControlStateNormal];
  278. UIBarButtonItem *menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
  279. self.navigationItem.rightBarButtonItem = menuButton2;
  280. }
  281. /**
  282. 取消搜索函数
  283. */
  284. -(void)cancelSearch
  285. {
  286. [_searchBar resignFirstResponder];
  287. [self removeCancel];
  288. }
  289. /**
  290. 移除取消按钮
  291. */
  292. -(void) removeCancel
  293. {
  294. self.navigationItem.rightBarButtonItem = nil;
  295. }
  296. @end