EditStringVC.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. //
  2. // EditStringVC.m
  3. // shiku_im
  4. //
  5. // Created by huangyp-pc on 16/11/23.
  6. // Copyright © 2016年 Reese. All rights reserved.
  7. //
  8. #import "EditStringVC.h"
  9. #import "EditCell.h"
  10. #import "AFNetworking.h"
  11. static NSString *cellID = @"cellID";
  12. static NSString *headerID = @"headerID";
  13. @interface EditStringVC () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
  14. @property (nonatomic,strong)UICollectionView *collectionView;
  15. @property (nonatomic,strong)NSMutableArray *SelectedArr; //保存选择的cell
  16. @property (nonatomic,strong)UICollectionView *secondCollectionV;
  17. @property (nonatomic,strong)NSMutableArray *selectedHideArr; //保存隐藏的cell
  18. @property (nonatomic,strong) UIScrollView *scrollV;
  19. @end
  20. @implementation EditStringVC
  21. {
  22. MBProgressHUD * HUD; //菊花
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.title = @"编辑字段";
  27. self.view.backgroundColor = Color(235, 233, 233, 1);
  28. _SelectedArr = [[NSMutableArray alloc]init];
  29. _selectedHideArr = [[NSMutableArray alloc]init];
  30. int cellHeight = [_dataArr[0] count]/3 + 1 + [_dataArr[1] count]/3 + 1;
  31. if ([_dataArr[0] count] == 0 && [_dataArr[1] count] ==0) {
  32. cellHeight = 0;
  33. }
  34. int cellHeight2 = 0;
  35. for (int i = 0; i<_hideDataArr.count; i++) {
  36. NSArray *a = _hideDataArr[i];
  37. cellHeight2 += (a.count/3 + 1);
  38. }
  39. UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
  40. btn2.frame = CGRectMake(0, 5, 22, 22);
  41. UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, iOS11?5:0, 22, 22)];
  42. image.image = [UIImage imageNamed:@"icon_back_nor3"];
  43. [btn2 addSubview:image];
  44. [btn2 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  45. UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc]initWithCustomView:btn2];
  46. self.navigationItem.leftBarButtonItems = @[bbi2];
  47. _scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-50)];
  48. _scrollV.showsHorizontalScrollIndicator = NO;
  49. _scrollV.showsVerticalScrollIndicator = NO;
  50. // 点击状态栏时自动滑动到顶部
  51. _scrollV.scrollsToTop = YES;
  52. _scrollV.backgroundColor = BGB_COLOR;
  53. [self.view addSubview:_scrollV];
  54. [_scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.left.right.top.equalTo(self.view);
  56. make.bottom.equalTo(self.view).offset(-50);
  57. }];
  58. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  59. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, cellHeight*30+90+30) collectionViewLayout:flowLayout];
  60. _collectionView.dataSource = self;
  61. _collectionView.delegate = self;
  62. _collectionView.backgroundColor = Color(250, 249, 249, 1);
  63. [_scrollV addSubview:_collectionView];
  64. [_collectionView registerClass:[EditCell class] forCellWithReuseIdentifier:cellID];
  65. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
  66. UICollectionViewFlowLayout *flowLayout2 = [[UICollectionViewFlowLayout alloc] init];
  67. _secondCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_collectionView.frame)+8, SCREEN_WIDTH, cellHeight2*30+90+30) collectionViewLayout:flowLayout2];
  68. _secondCollectionV.dataSource = self;
  69. _secondCollectionV.delegate = self;
  70. _secondCollectionV.backgroundColor = Color(250, 249, 249, 1);
  71. [_scrollV addSubview:_secondCollectionV];
  72. [_secondCollectionV registerClass:[EditCell class] forCellWithReuseIdentifier:cellID];
  73. [_secondCollectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
  74. _scrollV.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(_secondCollectionV.frame)+10);
  75. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  76. [btn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
  77. [self.view addSubview:btn];
  78. [btn setBackgroundColor:HexColor(@"#33A2EE") forState:UIControlStateNormal];
  79. [btn setBackgroundColor:HexColor(@"#2D84C0") forState:UIControlStateHighlighted];
  80. [btn setTitleColor:HexColor(@"#FFFFFF") forState:UIControlStateNormal];
  81. [btn setTitle:@"确认" forState:UIControlStateNormal];
  82. btn.titleLabel.font = FONT_SIZE(18);
  83. btn.layer.cornerRadius = 5;
  84. btn.layer.masksToBounds = YES;
  85. [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  86. make.left.equalTo(self.view).offset(20);
  87. make.right.equalTo(self.view).offset(-20);
  88. make.bottom.equalTo(self.view).offset(-10);
  89. make.height.mas_equalTo(40);
  90. }];
  91. HUD = [[MBProgressHUD alloc]initWithView:self.view];
  92. HUD.dimBackground = YES;
  93. HUD.mode = MBProgressHUDModeIndeterminate;
  94. [self.view addSubview:HUD];
  95. }
  96. #pragma mark - UICollectionView代理
  97. // 多少组
  98. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  99. {
  100. if (collectionView == _collectionView) {
  101. return _dataArr.count;
  102. }
  103. else if (collectionView == _secondCollectionV) {
  104. return _hideDataArr.count;
  105. }
  106. else
  107. return 0;
  108. }
  109. // 一组多少个
  110. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  111. {
  112. if (collectionView == _collectionView) {
  113. NSArray *sub = _dataArr[section];
  114. return sub.count;
  115. }
  116. else if (collectionView == _secondCollectionV) {
  117. NSArray *sub = _hideDataArr[section];
  118. return sub.count;
  119. }
  120. else
  121. return 0;
  122. }
  123. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  124. {
  125. EditCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  126. if (collectionView == _collectionView) {
  127. NSString *titleName = [NSString stringWithFormat:@"%@",_dataArr[indexPath.section][indexPath.row][@"fd_caption"]];
  128. if ([titleName isEqualToString:@"(null)"]) {
  129. titleName = [NSString stringWithFormat:@"%@",_dataArr[indexPath.section][indexPath.row][@"dg_caption"]];
  130. }
  131. cell.title.text = titleName;
  132. if ([_SelectedArr containsObject:_dataArr[indexPath.section][indexPath.row]]) {
  133. cell.bgView.image = [UIImage imageNamed:@"Collection_bg_select"];
  134. }else{
  135. cell.bgView.image = [UIImage imageNamed:@"Collection_bg_nor"];
  136. }
  137. return cell;
  138. }
  139. else if (collectionView == _secondCollectionV) {
  140. NSString *titleName = [NSString stringWithFormat:@"%@",_hideDataArr[indexPath.section][indexPath.row][@"fd_caption"]];
  141. if ([titleName isEqualToString:@"(null)"]) {
  142. titleName = [NSString stringWithFormat:@"%@",_hideDataArr[indexPath.section][indexPath.row][@"dg_caption"]];
  143. }
  144. cell.title.text = titleName;
  145. if ([_selectedHideArr containsObject:_hideDataArr[indexPath.section][indexPath.row]]) {
  146. cell.bgView.image = [UIImage imageNamed:@"Collection_bg_select"];
  147. }else{
  148. cell.bgView.image = [UIImage imageNamed:@"Collection_bg_nor"];
  149. }
  150. return cell;
  151. }
  152. else
  153. return cell;
  154. }
  155. // 设置组头或组尾
  156. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  157. {
  158. // 判断是组头还是组尾
  159. if ([kind isEqualToString:UICollectionElementKindSectionHeader])
  160. {
  161. // 去队列中取
  162. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
  163. headerView.backgroundColor = [UIColor clearColor];
  164. UILabel *lbl1 = [[UILabel alloc]init];
  165. lbl1.textColor = Color(47, 149, 221, 1);
  166. lbl1.font = FONT_SIZE(12);
  167. if (collectionView == _collectionView) {
  168. if (indexPath.section == 0) {
  169. lbl1.frame = CGRectMake(10, 35, 70, 20);
  170. lbl1.text = @"基本信息";
  171. UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
  172. view.backgroundColor = Color(245, 244, 244, 1);
  173. [headerView addSubview:view];
  174. UILabel *first = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 90, 30)];
  175. first.text = @"添加字段";
  176. first.textColor = Color(87, 87, 87, 1);
  177. first.font = FONT_SIZE(14);
  178. [view addSubview:first];
  179. UILabel *second = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, SCREEN_WIDTH-100, 30)];
  180. second.text = @"点击即为添加成功,再次点击即为取消";
  181. second.textColor = Color(150, 149, 149, 1);
  182. second.font = FONT_SIZE(12);
  183. [view addSubview:second];
  184. }
  185. else{
  186. lbl1.frame = CGRectMake(10, 5, 70, 20);
  187. lbl1.text = @"明细";
  188. }
  189. [headerView addSubview:lbl1];
  190. }
  191. else if (collectionView == _secondCollectionV) {
  192. if (indexPath.section == 0) {
  193. lbl1.frame = CGRectMake(10, 40, 70, 20);
  194. lbl1.text = @"基本信息";
  195. UIView *oneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 8)];
  196. oneView.backgroundColor = Color(235, 233, 233, 1);
  197. [headerView addSubview:oneView];
  198. UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 8, SCREEN_WIDTH, 30)];
  199. view.backgroundColor = Color(245, 244, 244, 1);
  200. [headerView addSubview:view];
  201. UILabel *first = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 30)];
  202. first.text = @"隐藏字段";
  203. first.textColor = Color(87, 87, 87, 1);
  204. first.font = FONT_SIZE(14);
  205. [view addSubview:first];
  206. UILabel *second = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, SCREEN_WIDTH-100, 30)];
  207. second.text = @"点击即为隐藏成功,再次点击即为取消";
  208. second.textColor = Color(150, 149, 149, 1);
  209. second.font = FONT_SIZE(12);
  210. [view addSubview:second];
  211. }
  212. else{
  213. lbl1.frame = CGRectMake(10, 5, 70, 20);
  214. lbl1.text = @"明细";
  215. }
  216. [headerView addSubview:lbl1];
  217. }
  218. return headerView;
  219. }
  220. else
  221. {
  222. return nil;
  223. }
  224. }
  225. // 返回组头的高度
  226. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  227. {
  228. if (section == 0) {
  229. return CGSizeMake(collectionView.bounds.size.width, 60);
  230. }
  231. else
  232. return CGSizeMake(collectionView.bounds.size.width, 30);
  233. }
  234. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  235. {
  236. if (collectionView == _collectionView) {
  237. if([_SelectedArr containsObject:_dataArr[indexPath.section][indexPath.row]])
  238. {
  239. [_SelectedArr removeObject:_dataArr[indexPath.section][indexPath.row]];
  240. }
  241. else
  242. {
  243. [_SelectedArr addObject:_dataArr[indexPath.section][indexPath.row]];
  244. }
  245. [collectionView reloadItemsAtIndexPaths:@[indexPath]];
  246. }
  247. else if (collectionView == _secondCollectionV) {
  248. if([_selectedHideArr containsObject:_hideDataArr[indexPath.section][indexPath.row]])
  249. {
  250. [_selectedHideArr removeObject:_hideDataArr[indexPath.section][indexPath.row]];
  251. }
  252. else
  253. {
  254. [_selectedHideArr addObject:_hideDataArr[indexPath.section][indexPath.row]];
  255. }
  256. [collectionView reloadItemsAtIndexPaths:@[indexPath]];
  257. }
  258. }
  259. // 设置每个item的大小
  260. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  261. {
  262. CGFloat width = (SCREEN_WIDTH-40) / 3;
  263. CGFloat height = 30;
  264. return CGSizeMake(width, height);
  265. }
  266. // 设置每个item之间的间距
  267. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  268. {
  269. // return UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
  270. return UIEdgeInsetsMake(0, 10, 0, 10);
  271. }
  272. - (void)didReceiveMemoryWarning {
  273. [super didReceiveMemoryWarning];
  274. // Dispose of any resources that can be recreated.
  275. }
  276. -(void)doneAction //确定按钮
  277. {
  278. // DLog(@"选了什么东西:%@",_SelectedArr);
  279. [HUD show:YES];
  280. NSString *formStr = @"";
  281. NSString *gridStr = @"";
  282. /**旧代码**
  283. int formCount = 0;
  284. int gridCount = 0;
  285. for (int i = 0; i < _SelectedArr.count; i++) {
  286. NSDictionary *dic = _SelectedArr[i];
  287. NSArray *keys = [dic allKeys];
  288. if ([keys containsObject:@"fd_field"]) {
  289. formCount ++;
  290. }
  291. }
  292. gridCount = _SelectedArr.count - formCount;
  293. for (int i = 0; i < _SelectedArr.count; i++) {
  294. NSDictionary *dic = _SelectedArr[i];
  295. for (id obj in dic) {
  296. if ([obj isEqualToString:@"fd_field"]) {
  297. formStr = [formStr stringByAppendingString:[dic objectForKey:obj]];
  298. if (i < (formCount - 1)) {
  299. formStr = [formStr stringByAppendingString:@","];
  300. }
  301. }
  302. else if ([obj isEqualToString:@"dg_field"]) {
  303. gridStr = [gridStr stringByAppendingString:[dic objectForKey:obj]];
  304. if (i < (gridCount - 1)) {
  305. gridStr = [gridStr stringByAppendingString:@","];
  306. }
  307. }
  308. }
  309. }
  310. */
  311. NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]init];
  312. /***把隐藏字段显示出来***/
  313. for (int i = 0; i < _SelectedArr.count; i++) {
  314. NSDictionary *dic = _SelectedArr[i];
  315. for (id obj in dic) {
  316. if ([obj isEqualToString:@"fd_field"]) {
  317. formStr = [NSString stringWithFormat:@"%@",[dic objectForKey:obj]];
  318. NSDictionary *a = @{formStr:@"-1"};
  319. [dataDic addEntriesFromDictionary:a];
  320. }
  321. else if ([obj isEqualToString:@"dg_field"]) {
  322. gridStr = [NSString stringWithFormat:@"%@",[dic objectForKey:obj]];
  323. NSDictionary *a = @{gridStr:@"-1"};
  324. [dataDic addEntriesFromDictionary:a];
  325. }
  326. }
  327. }
  328. /***把显示字段隐藏起来***/
  329. for (int i = 0; i < _selectedHideArr.count; i++) {
  330. NSDictionary *dic = _selectedHideArr[i];
  331. for (id obj in dic) {
  332. if ([obj isEqualToString:@"fd_field"]) {
  333. formStr = [NSString stringWithFormat:@"%@",[dic objectForKey:obj]];
  334. NSDictionary *a = @{formStr:@"0"};
  335. [dataDic addEntriesFromDictionary:a];
  336. }
  337. else if ([obj isEqualToString:@"dg_field"]) {
  338. gridStr = [NSString stringWithFormat:@"%@",[dic objectForKey:obj]];
  339. NSDictionary *a = @{gridStr:@"0"};
  340. [dataDic addEntriesFromDictionary:a];
  341. }
  342. }
  343. }
  344. DLog(@"forStore----:%@",dataDic);
  345. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
  346. NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  347. NSString *sessionId=[[NSUserDefaults standardUserDefaults] stringForKey:@"sessionId"];
  348. NSString *ip=[UASUserInfo shareManager].urlIp;
  349. NSString *url = [NSString stringWithFormat:@"%@mobile/common/updatemobiledefault.action?caller=%@&formStore=%@&master=%@&sessionUser=%@&sessionId=%@",ip,_caller,str,g_master,g_sessionUser,sessionId];
  350. DLog(@"aaaaaaaa:%@",url);
  351. url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  352. UASNetworkManager *manager = [UASNetworkManager shareManager];
  353. //拼接sessionID
  354. NSString *paramSessionId=[@"" stringByAppendingFormat:@"JSESSIONID=%@",sessionId];
  355. //设置cookie
  356. [manager.requestSerializer setValue:paramSessionId forHTTPHeaderField:@"Cookie"];
  357. [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@",g_sessionUser] forHTTPHeaderField:@"sessionUser"];
  358. [manager request:url method:POST parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
  359. [HUD hide:YES];
  360. DLog(@"chenggong---:%@",responseObject);
  361. NSString *sessionId = [NSString stringWithFormat:@"%@",responseObject[@"sessionId"]];
  362. if (![sessionId isEqualToString:@"(null)"]) {
  363. [[NSUserDefaults standardUserDefaults] setObject:sessionId forKey:@"sessionId"];
  364. }
  365. self.block();
  366. [self.navigationController popViewControllerAnimated:YES];
  367. } failure:^(NSURLSessionDataTask *task, NSError *error) {
  368. [HUD hide:YES];
  369. NSData *returnData = [[error userInfo] objectForKey:@"com.alamofire.serialization.response.error.data"];
  370. if (returnData) {
  371. NSDictionary *content = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];//转换数据格式
  372. NSString *errorStr = [NSString stringWithFormat:@"%@",[content objectForKey:@"exceptionInfo"]];
  373. DLog(@"错误的原因:%@",errorStr);
  374. [JOShowMessageFromNavi showDropViewWithMessage:errorStr ToNavi:self.navigationController withColor:nil];
  375. }
  376. else{
  377. [JOShowMessageFromNavi showDropViewWithMessage:@"请检查网络情况" ToNavi:self.navigationController withColor:nil];
  378. }
  379. }];
  380. }
  381. -(void)backAction
  382. {
  383. [self.navigationController popViewControllerAnimated:YES];
  384. }
  385. @end