DepartmentSearchVC.m 8.8 KB

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