// // EditStringVC.m // shiku_im // // Created by huangyp-pc on 16/11/23. // Copyright © 2016年 Reese. All rights reserved. // #import "EditStringVC.h" #import "EditCell.h" #import "AFNetworking.h" static NSString *cellID = @"cellID"; static NSString *headerID = @"headerID"; @interface EditStringVC () @property (nonatomic,strong)UICollectionView *collectionView; @property (nonatomic,strong)NSMutableArray *SelectedArr; //保存选择的cell @property (nonatomic,strong)UICollectionView *secondCollectionV; @property (nonatomic,strong)NSMutableArray *selectedHideArr; //保存隐藏的cell @property (nonatomic,strong) UIScrollView *scrollV; @end @implementation EditStringVC { MBProgressHUD * HUD; //菊花 } - (void)viewDidLoad { [super viewDidLoad]; self.title = @"编辑字段"; self.view.backgroundColor = Color(235, 233, 233, 1); _SelectedArr = [[NSMutableArray alloc]init]; _selectedHideArr = [[NSMutableArray alloc]init]; int cellHeight = [_dataArr[0] count]/3 + 1 + [_dataArr[1] count]/3 + 1; if ([_dataArr[0] count] == 0 && [_dataArr[1] count] ==0) { cellHeight = 0; } int cellHeight2 = 0; for (int i = 0; i<_hideDataArr.count; i++) { NSArray *a = _hideDataArr[i]; cellHeight2 += (a.count/3 + 1); } UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem]; btn2.frame = CGRectMake(0, 5, 22, 22); UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, iOS11?5:0, 22, 22)]; image.image = [UIImage imageNamed:@"icon_back_nor3"]; [btn2 addSubview:image]; [btn2 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc]initWithCustomView:btn2]; self.navigationItem.leftBarButtonItems = @[bbi2]; _scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-50)]; _scrollV.showsHorizontalScrollIndicator = NO; _scrollV.showsVerticalScrollIndicator = NO; // 点击状态栏时自动滑动到顶部 _scrollV.scrollsToTop = YES; _scrollV.backgroundColor = BGB_COLOR; [self.view addSubview:_scrollV]; [_scrollV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.equalTo(self.view); make.bottom.equalTo(self.view).offset(-50); }]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, cellHeight*30+90+30) collectionViewLayout:flowLayout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = Color(250, 249, 249, 1); [_scrollV addSubview:_collectionView]; [_collectionView registerClass:[EditCell class] forCellWithReuseIdentifier:cellID]; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID]; UICollectionViewFlowLayout *flowLayout2 = [[UICollectionViewFlowLayout alloc] init]; _secondCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_collectionView.frame)+8, SCREEN_WIDTH, cellHeight2*30+90+30) collectionViewLayout:flowLayout2]; _secondCollectionV.dataSource = self; _secondCollectionV.delegate = self; _secondCollectionV.backgroundColor = Color(250, 249, 249, 1); [_scrollV addSubview:_secondCollectionV]; [_secondCollectionV registerClass:[EditCell class] forCellWithReuseIdentifier:cellID]; [_secondCollectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID]; _scrollV.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(_secondCollectionV.frame)+10); UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; [btn setBackgroundColor:HexColor(@"#33A2EE") forState:UIControlStateNormal]; [btn setBackgroundColor:HexColor(@"#2D84C0") forState:UIControlStateHighlighted]; [btn setTitleColor:HexColor(@"#FFFFFF") forState:UIControlStateNormal]; [btn setTitle:@"确认" forState:UIControlStateNormal]; btn.titleLabel.font = FONT_SIZE(18); btn.layer.cornerRadius = 5; btn.layer.masksToBounds = YES; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(20); make.right.equalTo(self.view).offset(-20); make.bottom.equalTo(self.view).offset(-10); make.height.mas_equalTo(40); }]; HUD = [[MBProgressHUD alloc]initWithView:self.view]; HUD.dimBackground = YES; HUD.mode = MBProgressHUDModeIndeterminate; [self.view addSubview:HUD]; } #pragma mark - UICollectionView代理 // 多少组 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { if (collectionView == _collectionView) { return _dataArr.count; } else if (collectionView == _secondCollectionV) { return _hideDataArr.count; } else return 0; } // 一组多少个 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (collectionView == _collectionView) { NSArray *sub = _dataArr[section]; return sub.count; } else if (collectionView == _secondCollectionV) { NSArray *sub = _hideDataArr[section]; return sub.count; } else return 0; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { EditCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; if (collectionView == _collectionView) { NSString *titleName = [NSString stringWithFormat:@"%@",_dataArr[indexPath.section][indexPath.row][@"fd_caption"]]; if ([titleName isEqualToString:@"(null)"]) { titleName = [NSString stringWithFormat:@"%@",_dataArr[indexPath.section][indexPath.row][@"dg_caption"]]; } cell.title.text = titleName; if ([_SelectedArr containsObject:_dataArr[indexPath.section][indexPath.row]]) { cell.bgView.image = [UIImage imageNamed:@"Collection_bg_select"]; }else{ cell.bgView.image = [UIImage imageNamed:@"Collection_bg_nor"]; } return cell; } else if (collectionView == _secondCollectionV) { NSString *titleName = [NSString stringWithFormat:@"%@",_hideDataArr[indexPath.section][indexPath.row][@"fd_caption"]]; if ([titleName isEqualToString:@"(null)"]) { titleName = [NSString stringWithFormat:@"%@",_hideDataArr[indexPath.section][indexPath.row][@"dg_caption"]]; } cell.title.text = titleName; if ([_selectedHideArr containsObject:_hideDataArr[indexPath.section][indexPath.row]]) { cell.bgView.image = [UIImage imageNamed:@"Collection_bg_select"]; }else{ cell.bgView.image = [UIImage imageNamed:@"Collection_bg_nor"]; } return cell; } else return cell; } // 设置组头或组尾 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { // 判断是组头还是组尾 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { // 去队列中取 UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath]; headerView.backgroundColor = [UIColor clearColor]; UILabel *lbl1 = [[UILabel alloc]init]; lbl1.textColor = Color(47, 149, 221, 1); lbl1.font = FONT_SIZE(12); if (collectionView == _collectionView) { if (indexPath.section == 0) { lbl1.frame = CGRectMake(10, 35, 70, 20); lbl1.text = @"基本信息"; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)]; view.backgroundColor = Color(245, 244, 244, 1); [headerView addSubview:view]; UILabel *first = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 90, 30)]; first.text = @"添加字段"; first.textColor = Color(87, 87, 87, 1); first.font = FONT_SIZE(14); [view addSubview:first]; UILabel *second = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, SCREEN_WIDTH-100, 30)]; second.text = @"点击即为添加成功,再次点击即为取消"; second.textColor = Color(150, 149, 149, 1); second.font = FONT_SIZE(12); [view addSubview:second]; } else{ lbl1.frame = CGRectMake(10, 5, 70, 20); lbl1.text = @"明细"; } [headerView addSubview:lbl1]; } else if (collectionView == _secondCollectionV) { if (indexPath.section == 0) { lbl1.frame = CGRectMake(10, 40, 70, 20); lbl1.text = @"基本信息"; UIView *oneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 8)]; oneView.backgroundColor = Color(235, 233, 233, 1); [headerView addSubview:oneView]; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 8, SCREEN_WIDTH, 30)]; view.backgroundColor = Color(245, 244, 244, 1); [headerView addSubview:view]; UILabel *first = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 30)]; first.text = @"隐藏字段"; first.textColor = Color(87, 87, 87, 1); first.font = FONT_SIZE(14); [view addSubview:first]; UILabel *second = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, SCREEN_WIDTH-100, 30)]; second.text = @"点击即为隐藏成功,再次点击即为取消"; second.textColor = Color(150, 149, 149, 1); second.font = FONT_SIZE(12); [view addSubview:second]; } else{ lbl1.frame = CGRectMake(10, 5, 70, 20); lbl1.text = @"明细"; } [headerView addSubview:lbl1]; } return headerView; } else { return nil; } } // 返回组头的高度 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 0) { return CGSizeMake(collectionView.bounds.size.width, 60); } else return CGSizeMake(collectionView.bounds.size.width, 30); } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (collectionView == _collectionView) { if([_SelectedArr containsObject:_dataArr[indexPath.section][indexPath.row]]) { [_SelectedArr removeObject:_dataArr[indexPath.section][indexPath.row]]; } else { [_SelectedArr addObject:_dataArr[indexPath.section][indexPath.row]]; } [collectionView reloadItemsAtIndexPaths:@[indexPath]]; } else if (collectionView == _secondCollectionV) { if([_selectedHideArr containsObject:_hideDataArr[indexPath.section][indexPath.row]]) { [_selectedHideArr removeObject:_hideDataArr[indexPath.section][indexPath.row]]; } else { [_selectedHideArr addObject:_hideDataArr[indexPath.section][indexPath.row]]; } [collectionView reloadItemsAtIndexPaths:@[indexPath]]; } } // 设置每个item的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat width = (SCREEN_WIDTH-40) / 3; CGFloat height = 30; return CGSizeMake(width, height); } // 设置每个item之间的间距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { // return UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>) return UIEdgeInsetsMake(0, 10, 0, 10); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)doneAction //确定按钮 { // DLog(@"选了什么东西:%@",_SelectedArr); [HUD show:YES]; NSString *formStr = @""; NSString *gridStr = @""; /**旧代码** int formCount = 0; int gridCount = 0; for (int i = 0; i < _SelectedArr.count; i++) { NSDictionary *dic = _SelectedArr[i]; NSArray *keys = [dic allKeys]; if ([keys containsObject:@"fd_field"]) { formCount ++; } } gridCount = _SelectedArr.count - formCount; for (int i = 0; i < _SelectedArr.count; i++) { NSDictionary *dic = _SelectedArr[i]; for (id obj in dic) { if ([obj isEqualToString:@"fd_field"]) { formStr = [formStr stringByAppendingString:[dic objectForKey:obj]]; if (i < (formCount - 1)) { formStr = [formStr stringByAppendingString:@","]; } } else if ([obj isEqualToString:@"dg_field"]) { gridStr = [gridStr stringByAppendingString:[dic objectForKey:obj]]; if (i < (gridCount - 1)) { gridStr = [gridStr stringByAppendingString:@","]; } } } } */ NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]init]; /***把隐藏字段显示出来***/ for (int i = 0; i < _SelectedArr.count; i++) { NSDictionary *dic = _SelectedArr[i]; for (id obj in dic) { if ([obj isEqualToString:@"fd_field"]) { formStr = [NSString stringWithFormat:@"%@",[dic objectForKey:obj]]; NSDictionary *a = @{formStr:@"-1"}; [dataDic addEntriesFromDictionary:a]; } else if ([obj isEqualToString:@"dg_field"]) { gridStr = [NSString stringWithFormat:@"%@",[dic objectForKey:obj]]; NSDictionary *a = @{gridStr:@"-1"}; [dataDic addEntriesFromDictionary:a]; } } } /***把显示字段隐藏起来***/ for (int i = 0; i < _selectedHideArr.count; i++) { NSDictionary *dic = _selectedHideArr[i]; for (id obj in dic) { if ([obj isEqualToString:@"fd_field"]) { formStr = [NSString stringWithFormat:@"%@",[dic objectForKey:obj]]; NSDictionary *a = @{formStr:@"0"}; [dataDic addEntriesFromDictionary:a]; } else if ([obj isEqualToString:@"dg_field"]) { gridStr = [NSString stringWithFormat:@"%@",[dic objectForKey:obj]]; NSDictionary *a = @{gridStr:@"0"}; [dataDic addEntriesFromDictionary:a]; } } } DLog(@"forStore----:%@",dataDic); NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil]; NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *sessionId=[[NSUserDefaults standardUserDefaults] stringForKey:@"sessionId"]; NSString *ip=[UASUserInfo shareManager].urlIp; NSString *url = [NSString stringWithFormat:@"%@mobile/common/updatemobiledefault.action?caller=%@&formStore=%@&master=%@&sessionUser=%@&sessionId=%@",ip,_caller,str,g_master,g_sessionUser,sessionId]; DLog(@"aaaaaaaa:%@",url); url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; UASNetworkManager *manager = [UASNetworkManager shareManager]; //拼接sessionID NSString *paramSessionId=[@"" stringByAppendingFormat:@"JSESSIONID=%@",sessionId]; //设置cookie [manager.requestSerializer setValue:paramSessionId forHTTPHeaderField:@"Cookie"]; [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@",g_sessionUser] forHTTPHeaderField:@"sessionUser"]; [manager request:url method:POST parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) { [HUD hide:YES]; DLog(@"chenggong---:%@",responseObject); NSString *sessionId = [NSString stringWithFormat:@"%@",responseObject[@"sessionId"]]; if (![sessionId isEqualToString:@"(null)"]) { [[NSUserDefaults standardUserDefaults] setObject:sessionId forKey:@"sessionId"]; } self.block(); [self.navigationController popViewControllerAnimated:YES]; } failure:^(NSURLSessionDataTask *task, NSError *error) { [HUD hide:YES]; NSData *returnData = [[error userInfo] objectForKey:@"com.alamofire.serialization.response.error.data"]; if (returnData) { NSDictionary *content = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];//转换数据格式 NSString *errorStr = [NSString stringWithFormat:@"%@",[content objectForKey:@"exceptionInfo"]]; DLog(@"错误的原因:%@",errorStr); [JOShowMessageFromNavi showDropViewWithMessage:errorStr ToNavi:self.navigationController withColor:nil]; } else{ [JOShowMessageFromNavi showDropViewWithMessage:@"请检查网络情况" ToNavi:self.navigationController withColor:nil]; } }]; } -(void)backAction { [self.navigationController popViewControllerAnimated:YES]; } @end