// // BusinessDepartmentTreeViewController.m // IBOSS // // Created by guan hong hou on 2017/9/7. // Copyright © 2017年 elongtian. All rights reserved. // // 功能描述:业务部门树型控制器 #import "BusinessDepartmentTreeViewController.h" #import "AchiementDepartmentInfoModel.h" #import "Util.h" #import "BusinessDepartmentTreeTableViewCell.h" #import "BusinessDepartmentFrame.h" @interface BusinessDepartmentTreeViewController (){ /** 业务部门tableview数据源 */ NSMutableArray *dataSource; } @end @implementation BusinessDepartmentTreeViewController #pragma 公共函数 - (void)viewDidLoad { [super viewDidLoad]; dataSource = [NSMutableArray new]; [self initUI]; [self initData]; } #pragma 委托函数 /** 数据成功回调 @param sender sender description */ - (void)onRequestSuccess:(ASIDownManager *)sender { // 取消进度条 [self cancel]; // 服务器返回数据 RequestResultModel *resultModel = [RequestResultModel dk_modelWithJSON:sender.mWebStr]; // 服务器返回数据状态值 int iStatus = resultModel.status; // 服务器返回数据消息 NSString *message = resultModel.message; // 服务器返回数据状态值正确 if (iStatus == 0) { // 服务器返回数据结果 NSArray *approvArr = (NSArray *)resultModel.result; if(approvArr != nil&&approvArr.count>0) { _arrSearch=[[NSMutableArray alloc]init]; _treeArray=[[NSMutableArray alloc]init]; for(int i=0;i */ - (void)onRequestFail:(ASIDownManager *)sender { [self cancel]; [self showAlertViewText:@"网络异常"]; } /** table view的分区数 @param tableView <#tableView description#> @return <#return value description#> */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return dataSource.count; } /** tableview cell的高度 @param tableView @param indexPath @return */ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSMutableArray *arr = [dataSource objectAtIndex:indexPath.section]; BusinessDepartmentFrame *testFrame = [arr objectAtIndex:indexPath.row]; return testFrame.cellHeight; } /** table view的行数 @param tableView <#tableView description#> @param section <#section description#> @return <#return value description#> */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSMutableArray *models = [dataSource objectAtIndex:section]; return models.count; } /** 加载tableview cell @param tableView <#tableView description#> @param indexPath <#indexPath description#> @return <#return value description#> */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"CELLID"; BusinessDepartmentTreeTableViewCell *cell = [[BusinessDepartmentTreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; NSMutableArray *arr = [dataSource objectAtIndex:indexPath.section]; BusinessDepartmentFrame *testFrame = [arr objectAtIndex:indexPath.row]; [cell setUIWithModel:testFrame]; [cell.checkBT addTarget:self action:@selector(selectBTClick:event:) forControlEvents:UIControlEventTouchUpInside]; return cell; } /** table view的点击事件 @param tableView <#tableView description#> @param indexPath <#indexPath description#> */ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSInteger section= indexPath.section; NSMutableArray *array = [dataSource objectAtIndex:section]; NSInteger row= indexPath.row; BusinessDepartmentFrame *tes = [array objectAtIndex:row]; if(tes!=nil){ if(tes.departmentModel!=nil){ if (tes.departmentModel.childArray!=nil&&tes.departmentModel.childArray.count>0) { tes.departmentModel.isExpand = !tes.departmentModel.isExpand; [self reloadSection:section withMyTestFrame:tes withExpand:tes.departmentModel.isExpand]; } } } } /** tableview的header高度 @param tableView <#tableView description#> @param section <#section description#> @return <#return value description#> */ - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.01; } /** table view的footer 高度 @param tableView <#tableView description#> @param section <#section description#> @return <#return value description#> */ - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.01; } #pragma 私有函数 /** 初始化UI */ -(void)initUI{ if (_isPresentViewFlag) { UIView *vTitle = [[UIView alloc]init]; vTitle.frame = CGRectMake(0, 0, Screen_Width, 44 + rectStatusHeight); vTitle.backgroundColor = NavigationBarTintColor; [self.view addSubview:vTitle]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(20, rectStatusHeight + 13, 9, 15); [vTitle addSubview:button]; UILabel *lblTitle = [[UILabel alloc]init]; lblTitle.frame = CGRectMake(9, rectStatusHeight + 13, Screen_Width-2*9 - 20, 15); lblTitle.textColor = [UIColor whiteColor]; lblTitle.text = @"请选择"; lblTitle.textAlignment = NSTextAlignmentCenter; [vTitle addSubview:lblTitle]; UIButton *btnSure=[UIButton buttonWithType:UIButtonTypeCustom]; [btnSure setTitle:@"确定" forState:UIControlStateNormal]; [btnSure addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside]; btnSure.titleLabel.font= [UIFont systemFontOfSize: 15.0]; btnSure.frame = CGRectMake(CGRectGetWidth(vTitle.frame)-60, rectStatusHeight + 13,40, 15); [vTitle addSubview:btnSure]; } else { self.navigationItem.title = @"请选择"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0,45,22); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; [self.navigationController setNavigationBarHidden:NO]; } _departmentTableView = [[UITableView alloc] initWithFrame:CGRectZero]; if (_isPresentViewFlag) { self.departmentTableView.frame = CGRectMake(0, 44 + rectStatusHeight, self.view.bounds.size.width, self.view.bounds.size.height - 44 - rectStatusHeight - 1) ; } else{ self.departmentTableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-rectNavHeight - rectStatusHeight - 1) ; } _departmentTableView.separatorStyle=UITableViewCellSeparatorStyleNone; _departmentTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight; _departmentTableView.delegate = self; _departmentTableView.dataSource=self; [self.view addSubview: _departmentTableView]; } /** 自动展开列表 */ -(void)autoExpandTree{ NSInteger section= 0; NSMutableArray *array = [dataSource objectAtIndex:section]; NSInteger row= 0; BusinessDepartmentFrame *frm = [array objectAtIndex:row]; if (frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray.count>0) { frm.departmentModel.isExpand = !frm.departmentModel.isExpand; [self reloadSection:section withMyTestFrame:frm withExpand:frm.departmentModel.isExpand]; } } /** 返回按钮 */ -(void)goBack{ if (_isPresentViewFlag) { [self dismissViewControllerAnimated:YES completion:nil]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } /** 初始化数据 */ -(void)initData{ self.downManager = [[ASIDownManager alloc] init]; self.downManager.delegate=self; self.downManager.onRequestSuccess=@selector(onRequestSuccess:); self.downManager.onRequestFail=@selector(onRequestFail:); [self startLoading]; NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary new]; [dict setObject:@"GetOrganizationDataSource" forKey:@"Action"]; [dict setObject:@"" forKey:@"OrganizationName"]; [dict setObject:@"" forKey:@"OrganizationCode"]; [dict setObject:kkAccountCode forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [self.downManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; } /** 进度条隐藏 */ - (void)cancel { [self stopLoading]; } /** 确定按钮 */ -(void)submit{ _departmentCodeResultArray=[[NSMutableArray alloc]init]; _departmentNameResultArray=[[NSMutableArray alloc]init]; if(dataSource!=nil&&dataSource.count>0){ for (NSMutableArray *arr0 in dataSource) { [self getAllSelectedTreeArray:arr0]; } NSString *departmentCodeResultStr=[_departmentCodeResultArray componentsJoinedByString:@","]; NSString *departmentNameResultStr=[_departmentNameResultArray componentsJoinedByString:@","]; if ([self.cDelegate respondsToSelector:@selector(achievementDataTreeResult: name:)]) { [self.cDelegate achievementDataTreeResult:departmentCodeResultStr name:departmentNameResultStr]; } //是否是dismissViewController if (_isPresentViewFlag) { [self dismissViewControllerAnimated:YES completion:nil]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } } /** 获取所有的选中的列表 @param treeArray <#treeArray description#> */ -(void)getAllSelectedTreeArray:(NSMutableArray*) treeArray{ for(BusinessDepartmentFrame *frm in treeArray){ if(frm.departmentModel.isSelect){ NSString *organizationCode=frm.departmentModel.organizationCode; NSString *organizationName=frm.departmentModel.organizationName; if(![_departmentCodeResultArray containsObject:organizationCode]){ [_departmentCodeResultArray addObject:organizationCode]; } if(![_departmentNameResultArray containsObject:organizationName]){ [_departmentNameResultArray addObject:organizationName]; } if(frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray.count>0){ [self getAllSelectedTreeArray:frm.departmentModel.childArray]; } } } } /** 重新加载section @param section <#section description#> @param model <#model description#> @param isExpand <#isExpand description#> */ - (void)reloadSection:(NSInteger)section withMyTestFrame:(BusinessDepartmentFrame *)frm withExpand:(BOOL)isExpand{ NSMutableArray *arry1 = [dataSource objectAtIndex:section]; NSInteger index = [arry1 indexOfObject:frm]; NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index+1, frm.departmentModel.childArray.count)]; if (isExpand){ if(frm!=nil){ if(frm.departmentModel!=nil){ AchiementDepartmentInfoModel *model=frm.departmentModel; if(model.childArray!=nil&&model.childArray.count>0){ for (int i =0; i0)&&(arry1!=nil&&arry1.count>0)){ [self removeChild:arry1 frame:frm]; } } } } NSIndexSet *reloadSet = [NSIndexSet indexSetWithIndex:section]; [self.departmentTableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic]; } /** 收回列表 @param arry <#arry description#> @param model <#model description#> */ -(void)removeChild:(NSMutableArray*)arry frame:(BusinessDepartmentFrame *)frm{ if(frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray.count>0){ for(BusinessDepartmentFrame *childFrame in frm.departmentModel.childArray){ if([arry containsObject:childFrame]){ [arry removeObject:childFrame]; } if(childFrame.departmentModel.childArray!=nil&&childFrame.departmentModel.childArray.count>0){ [self removeChild:arry frame:childFrame]; } } } } /** check box点击事件 @param sender <#sender description#> */ - (void)selectBTClick:(UIButton*)sender event:(id)event{ UITableView *cell = (UITableView *)sender.superview.superview; NSSet *touches =[event allTouches]; UITouch *touch =[touches anyObject]; CGPoint currentTouchPosition = [touch locationInView:self.departmentTableView]; NSIndexPath *indexPath= [self.departmentTableView indexPathForRowAtPoint:currentTouchPosition]; BusinessDepartmentTreeTableViewCell *businessCell= [self.departmentTableView cellForRowAtIndexPath:indexPath]; businessCell.achievementModel.isSelect = !businessCell.achievementModel.isSelect; [businessCell.checkBT setImage:businessCell.achievementModel.isSelect ? [UIImage imageNamed:@"order_checked"]:[UIImage imageNamed:@"order_unchecked"] forState:UIControlStateNormal]; if(businessCell.achievementModel.childArray!=nil&&businessCell.achievementModel.childArray.count>0) { [self selectAllChildNode:businessCell.achievementModel.childArray achievementCell:&businessCell]; } if(dataSource!=nil&&dataSource.count>0){ for (NSMutableArray *arr0 in dataSource) { [self selectParentNode:arr0 achievementFrame:businessCell.achievementFrame]; } for (NSMutableArray *arr0 in dataSource) { [self deSelectParentNode:arr0 achievementFrame:businessCell.achievementFrame]; } } [self.departmentTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone]; } /** 选中和反选所有的子节点 @param childArray <#childArray description#> @param cell <#cell description#> */ -(void)selectAllChildNode:(NSMutableArray*)childArray achievementCell:(BusinessDepartmentTreeTableViewCell*__autoreleasing *)cell{ for(BusinessDepartmentFrame *frm in childArray){ if(frm!=nil){ BusinessDepartmentTreeTableViewCell* achievementCell=*cell; frm.departmentModel.isSelect=achievementCell.achievementModel.isSelect; if(frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray>0){ [self selectAllChildNode:frm.departmentModel.childArray achievementCell:&achievementCell]; } } } } /** 选中子节点的父节点 @param array <#array description#> @param achievementModel <#achievementModel description#> */ -(void)selectParentNode:(NSMutableArray*)array achievementFrame:(BusinessDepartmentFrame*)achievementFrame{ for(BusinessDepartmentFrame *frame in array){ if(frame!=nil){ if(frame.departmentModel!=nil){ if(frame.departmentModel.childArray!=nil&&frame.departmentModel.childArray.count>0){ if([frame.departmentModel.childArray containsObject:achievementFrame]){ if(achievementFrame.departmentModel.isSelect){ if([self isSelectAllChild:frame.departmentModel]){ frame.departmentModel.isSelect=YES; } if(dataSource!=nil&&dataSource.count>0){ for (NSMutableArray *arr0 in dataSource) { [self selectParentNode:arr0 achievementFrame:frame]; } } } } for(BusinessDepartmentFrame *mo in frame.departmentModel.childArray){ if(mo!=nil){ if(mo.departmentModel!=nil){ if(mo.departmentModel.childArray!=nil&&mo.departmentModel.childArray.count>0){ [self selectParentNode:mo.departmentModel.childArray achievementFrame:achievementFrame]; } } } } } } } } } /** 是否选中所有的子节点 @param model <#model description#> @return <#return value description#> */ -(Boolean)isSelectAllChild:(AchiementDepartmentInfoModel*)model{ int i=0; Boolean flag = NO; if(model.childArray!=nil&&model.childArray.count>0){ for(BusinessDepartmentFrame *frame in model.childArray){ if(frame.departmentModel.isSelect){ i++; } } if(model.childArray.count==i){ flag=YES; } else{ flag=NO; } } return flag; } /** 反选子节点的父节点 @param array <#array description#> @param achievementModel <#achievementModel description#> */ -(void)deSelectParentNode:(NSMutableArray*)array achievementFrame:(BusinessDepartmentFrame*)achievementFrame{ for(BusinessDepartmentFrame *frame in array){ if(frame.departmentModel.childArray!=nil&&frame.departmentModel.childArray.count>0){ if(frame!=nil){ if(frame.departmentModel!=nil){ if([frame.departmentModel.childArray containsObject:achievementFrame]){ if(!achievementFrame.departmentModel.isSelect){ frame.departmentModel.isSelect=NO; if(dataSource!=nil&&dataSource.count>0){ for (NSMutableArray *arr0 in dataSource) { [self deSelectParentNode:arr0 achievementFrame:frame]; } } } } for(BusinessDepartmentFrame *mo in frame.departmentModel.childArray){ if(mo!=nil){ if(mo.departmentModel!=nil){ if(mo.departmentModel.childArray!=nil&&mo.departmentModel.childArray.count>0){ [self deSelectParentNode:mo.departmentModel.childArray achievementFrame:achievementFrame]; } } } } } } } } } @end