| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- //
- // BuMenSearchTC.m
- // IBOSS
- //
- // Created by iHope on 14-7-11.
- // Copyright (c) 2014年 elongtian. All rights reserved.
- //
- #import "DepartmentSearchVC.h"
- #import "DepartmentSearchCell.h"
- @interface DepartmentSearchVC()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
- @property(nonatomic,strong) ASIDownManager *mDownManager;
- @property(strong) IBOutlet UISearchBar *searchBar;
- @property(strong) UITableView *tableView;
- @end
- @implementation DepartmentSearchVC
- @synthesize subview;
- #pragma mark - 公共函数
- /**
- viewWillAppear函数
- */
- -(void)viewWillAppear:(BOOL)animated
- {
-
- _searchArr=[NSMutableArray new];
- _filterArr=[NSMutableArray new];
- self.mDownManager = [[ASIDownManager alloc] init];
- _mDownManager.delegate = self;
- _mDownManager.OnImageDown = @selector(onLoadFinish:);
- _mDownManager.OnImageFail = @selector(onLoadFail:);
- [self reloadData];
-
- }
- /**
- viewDidLoad函数
- */
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.navigationItem.title=@"业务部门";
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"]
- forState:UIControlStateNormal];
- [button addTarget:self action:@selector(goBack)
- forControlEvents:UIControlEventTouchUpInside];
- button.frame = CGRectMake(0, 0, 15, 18);
-
- UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
- self.navigationItem.leftBarButtonItem = menuButton;
- self.navigationController.navigationBar.barTintColor = NavigationBarTintColor;
- self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:16]};
- _tableView=[UITableView new];
- _tableView.delegate=self;
- _tableView.dataSource=self;
- _tableView.frame=CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height) ;
- [self.view addSubview:_tableView];
- _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
- _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
- _searchBar.delegate=self;
- self.tableView.tableHeaderView=_searchBar;
-
-
- }
- /**
- dealloc函数
- */
- -(void)dealloc
- {
- [self cancel];
- }
- #pragma mark - 委托函数
- /**
- 部门数据加载完成
- */
- - (void)onLoadFinish:(ASIDownManager *)sender {
-
- NSDictionary *dic = [sender.mWebStr JSONValue];
- [self cancel];
- if (dic && [dic isKindOfClass:[NSDictionary class]]) {
- int iStatus = [[dic objectForKey:@"Status"] intValue];
- NSString *message=[dic objectForKey:@"Message"];
- if (iStatus == 0) {
- NSArray * approvArr=[dic objectForKey:@"Result"];
- if(approvArr!=nil)
- {
-
- _searchArr=[[NSMutableArray alloc]initWithArray:approvArr];
- _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr];
- if(_searchArr.count==0)
- {
- [self showAlertViewText:@"未找到匹配结果"];
-
- }
- [self.tableView reloadData];
- }
- }
- else if(iStatus==ActionResultStatusAuthError
- ||iStatus==ActionResultStatusNoLogin
- ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){
- [self showReLoginDialog:message];
- return;
- } else {
- [self.tableView reloadData];
- [self showAlertViewText:message];
-
- }
-
- }
- }
- //部门数据加载失败函数
- - (void)onLoadFail:(ASIDownManager *)sender {
- [self cancel];
- [self.tableView reloadData];
- [self showAlertViewText:@"加载失败"];
- }
- //tableview的高度
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 89.0f;
- }
- //tableview的分区数
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
-
- return 1;
- }
- //tableview的行数
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
-
- return _filterArr.count;
- }
- //获取tableviewcell
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- DepartmentSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
- if(cell==nil)
- {
-
- NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"DepartmentSearchCell" owner:self options:nil];
- cell = [nib objectAtIndex:0];
- }
- NSDictionary *dic=_filterArr[indexPath.row];
-
- NSString *name=[dic objectForKey:@"OrganizationName"];
- NSString *code=[dic objectForKey:@"OrganizationCode"];
- NSString *fullname=[dic objectForKey:@"OrganizationFullName"];
- cell.lblName.text=name;
- cell.lblCode.text=code;
- cell.lblFullName.text=fullname;
-
- return cell;
- }
- //tableviewcell的选中事件
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [self dismissViewControllerAnimated:YES completion:^(void){
- NSDictionary *dic=_filterArr[indexPath.row];
- NSString *organizationName=[dic objectForKey:@"OrganizationName"];
- NSString *organizationId=[dic objectForKey:@"OrganizationID"];
- NSString *organizationCode=[dic objectForKey:@"OrganizationCode"];
- if([self.departmentDelegate respondsToSelector:@selector(department:organizationCode:organizationName:)]){
- [self.departmentDelegate department:organizationId organizationCode:organizationCode organizationName:organizationName];
- }
-
- }];
-
- }
- //searchBar开始编辑
- - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
- {
- [self addCancelButton];
- return YES;
- }
- //允许替换文本
- - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- return YES;
- }
- //搜索文本输入完成事件
- - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
- {
-
- if ([searchText isEqualToString:@""]) {
- self.filterArr = _searchArr;
- [_tableView reloadData];
- return;
- }
-
- NSString *keyName = @"";
- keyName = @"OrganizationName";
- NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@ Or %K contains[cd] %@", keyName, searchText,@"OrganizationFullName",searchText];
-
- NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]];
-
- self.filterArr = filteredArray;
- [_tableView reloadData];
- }
- //取消按钮点击事件
- - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
- {
- [searchBar resignFirstResponder];
- [self cancelSearch];
- }
- //scrollview拖拽事件
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- {
- [_searchBar resignFirstResponder];
- [self cancelSearch];
- }
- #pragma mark - 私有函数
- //取消加载
- - (void)cancel {
- [self stopLoading];
- }
- //加载数据接口
- -(void)reloadData
- {
- [self startLoading];
- NSString *urlStr = ServerURL;
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
- [dict setObject:@"GetOrganizationDataSource" forKey:@"Action"];
- [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
- [dict setObject:kkUserCode forKey:@"UserCode"];
- [dict setObject:kkUserPwd forKey:@"UserPassword"];
- [dict setObject:kkSessionKey forKey:@"SessionKey"];
- [dict setObject:@"" forKey:@"OrganizationCode"];
- [dict setObject:@"" forKey:@"OrganizationName"];
- [dict setObject:@"true" forKey:@"IsOnlyDisplayEnd"];
- [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];
- }
- //返回按钮
- -(void)goBack
- {
- if(!subview){
- [self dismissViewControllerAnimated:YES completion:^(void){
- }];}
- else{
- [self.navigationController popViewControllerAnimated:YES];
- }}
- //增加取消按钮
- -(void)addCancelButton
- {
- //添加取消按钮
- UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
- [button2 addTarget:self action:@selector(cancelSearch)
- forControlEvents:UIControlEventTouchUpInside];
- button2.frame = CGRectMake(0, 0, 40, 24);
- [button2 setTitle:@"取消" forState:UIControlStateNormal];
- [button2 setTitleColor:NavBarItemColor forState:UIControlStateNormal];
- UIBarButtonItem *menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
- self.navigationItem.rightBarButtonItem = menuButton2;
- }
- //取消搜索
- -(void)cancelSearch
- {
- [_searchBar resignFirstResponder];
- [self removeCancel];
- }
- //移除取消按钮
- -(void) removeCancel
- {
- self.navigationItem.rightBarButtonItem = nil;
- }
- @end
|