AddGroupLabelVC.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // AddGroupLabelVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/5/8.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "AddGroupLabelVC.h"
  9. #import "TDGroupLabelCell.h"
  10. @interface AddGroupLabelVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>
  11. @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
  12. @property (weak, nonatomic) IBOutlet UIButton *doneBtn;
  13. @property (weak, nonatomic) IBOutlet UITextField *textField;
  14. @property (weak, nonatomic) IBOutlet UIButton *addBtn;
  15. @property (strong, nonatomic) NSMutableArray *dataArray;
  16. @end
  17. @implementation AddGroupLabelVC
  18. +(AddGroupLabelVC *)initAddGroupLabelVC{
  19. AddGroupLabelVC *controller = [StoryboardManager.shared.TDGroup instantiateViewControllerWithIdentifier:@"AddGroupLabelVC"];
  20. controller.modalPresentationStyle = UIModalPresentationFullScreen;
  21. return controller;
  22. }
  23. - (NSMutableArray *)dataArray
  24. {
  25. if (!_dataArray) {
  26. _dataArray = [NSMutableArray array];
  27. }
  28. return _dataArray;
  29. }
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. self.fd_prefersNavigationBarHidden = YES;
  33. self.collectionView.delegate = self;
  34. self.collectionView.dataSource = self;
  35. [self.collectionView setCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
  36. self.collectionView.showsVerticalScrollIndicator = NO;
  37. self.collectionView.showsHorizontalScrollIndicator = NO;
  38. self.collectionView.contentInset = UIEdgeInsetsMake(24, 30, 0, 24);
  39. [self.collectionView registerNib:[UINib nibWithNibName:@"TDGroupLabelCell" bundle:nil] forCellWithReuseIdentifier:@"TDGroupLabelCell"];
  40. self.collectionView.backgroundColor = [UIColor whiteColor];
  41. self.textField.returnKeyType = UIReturnKeyDone;
  42. self.textField.delegate = self;
  43. self.dataArray = [NSMutableArray arrayWithArray:self.array];
  44. [self.collectionView reloadData];
  45. WS(weakSelf);
  46. [self.addBtn setAction:^{
  47. if (weakSelf.textField.text.length > 0) {
  48. [weakSelf.dataArray addObject:weakSelf.textField.text];
  49. [weakSelf.textField resignFirstResponder];
  50. weakSelf.textField.text = @"";
  51. dispatch_async(dispatch_get_main_queue(), ^{
  52. [weakSelf.collectionView reloadData];
  53. });
  54. }
  55. }];
  56. [self.doneBtn setAction:^{
  57. [weakSelf doneAction];
  58. }];
  59. }
  60. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  61. {
  62. [self.textField resignFirstResponder];
  63. return YES;
  64. }
  65. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  66. return 1;
  67. }
  68. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  69. return self.dataArray.count;
  70. }
  71. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  72. {
  73. return CGSizeMake((SCREEN_WIDTH - 63) * 0.5, 58.f);
  74. }
  75. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  76. {
  77. return 7.f;
  78. }
  79. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  80. {
  81. return 9.f;
  82. }
  83. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  84. TDGroupLabelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TDGroupLabelCell" forIndexPath:indexPath];
  85. cell.titleL.text = self.dataArray[indexPath.item];
  86. cell.deleteBtn.hidden = NO;
  87. WS(weakSelf);
  88. [cell.deleteBtn setAction:^{
  89. [weakSelf.dataArray removeObjectAtIndex:indexPath.item];
  90. dispatch_async(dispatch_get_main_queue(), ^{
  91. [weakSelf.collectionView reloadData];
  92. });
  93. }];
  94. return cell;
  95. }
  96. - (void)doneAction
  97. {
  98. WS(weakSelf);
  99. NSString *string = [self.dataArray componentsJoinedByString:@","];
  100. [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_Update) parameters:@{@"LabelName":string,@"Id":@(self.Id)} responseStyle:DATA success:^(id _Nonnull responseObject) {
  101. if (weakSelf.refreshDataBlock) {
  102. weakSelf.refreshDataBlock(weakSelf.dataArray);
  103. }
  104. [weakSelf dismissViewControllerAnimated:YES completion:^{
  105. }];
  106. } failure:^(NSError * _Nonnull error) {
  107. SHOWERROR([ZYCTool handerResultData:error]);
  108. }];
  109. }
  110. @end