JXSelectLabelsVC.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // JXSelectLabelsVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2018/7/19.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXSelectLabelsVC.h"
  9. #import "JXLabelObject.h"
  10. #import "QCheckBox.h"
  11. #import "JXSelectLabelGroupCell.h"
  12. #define HEIGHT 60
  13. @interface JXSelectLabelsVC ()
  14. @property (nonatomic, strong) NSMutableArray *array;
  15. @property (nonatomic, strong) NSMutableArray *checkBoxArr;
  16. @end
  17. @implementation JXSelectLabelsVC
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.title = Localized(@"JX_SelectionLabel");
  22. self.heightHeader = JX_SCREEN_TOP;
  23. self.heightFooter = 0;
  24. self.isGotoBack = YES;
  25. self.isShowFooterPull = NO;
  26. self.isShowHeaderPull = NO;
  27. [self createHeadAndFoot];
  28. _array = [[JXLabelObject sharedInstance] fetchAllLabelsFromLocal];
  29. for (JXLabelObject *labelObj in _array) {
  30. NSString *userIdStr = labelObj.userIdList;
  31. NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  32. if (userIdStr.length <= 0) {
  33. userIds = nil;
  34. }
  35. NSMutableArray *newUserIds = [userIds mutableCopy];
  36. for (NSInteger i = 0; i < userIds.count; i ++) {
  37. NSString *userId = userIds[i];
  38. NSString *userName = [JXUserObject getUserNameWithUserId:userId];
  39. if (!userName || userName.length <= 0) {
  40. [newUserIds removeObject:userId];
  41. }
  42. }
  43. NSString *string = [newUserIds componentsJoinedByString:@","];
  44. labelObj.userIdList = string;
  45. [labelObj update];
  46. }
  47. _checkBoxArr = [NSMutableArray array];
  48. UIButton *allSelect = [UIButton buttonWithType:UIButtonTypeSystem];
  49. [allSelect setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  50. [allSelect setTitleColor:THESIMPLESTYLE ? [UIColor blackColor] : [UIColor whiteColor] forState:UIControlStateNormal];
  51. allSelect.tintColor = [UIColor clearColor];
  52. allSelect.frame = CGRectMake(JX_SCREEN_WIDTH - 70, JX_SCREEN_TOP - 34, 60, 24);
  53. [allSelect addTarget:self action:@selector(confirmBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  54. [self.tableHeader addSubview:allSelect];
  55. }
  56. - (void)confirmBtnAction:(UIButton *)btn {
  57. if ([self.delegate respondsToSelector:@selector(selectLabelsVC:selectLabelsArray:)]) {
  58. [self.delegate selectLabelsVC:self selectLabelsArray:_selLabels];
  59. }
  60. [self actionQuit];
  61. }
  62. - (void)didSelectedCheckBox:(QCheckBox *)checkbox checked:(BOOL)checked{
  63. JXLabelObject *labelObj = _array[checkbox.tag];
  64. if(checked){
  65. [_selLabels addObject:labelObj];
  66. }
  67. else{
  68. NSInteger index = -1;
  69. for (NSInteger i = 0; i < _selLabels.count; i ++) {
  70. JXLabelObject *selLabel = _selLabels[i];
  71. if ([selLabel.groupId isEqualToString:labelObj.groupId]) {
  72. index = i;
  73. break;
  74. }
  75. }
  76. if (index >= 0) {
  77. [_selLabels removeObjectAtIndex:index];
  78. }
  79. }
  80. }
  81. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  82. return _array.count;
  83. }
  84. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  85. JXLabelObject *label = _array[indexPath.row];
  86. NSString *userIdStr = label.userIdList;
  87. NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  88. if (userIdStr.length <= 0) {
  89. userIds = nil;
  90. }
  91. NSMutableString *userNameStr = [NSMutableString string];
  92. for (NSInteger i = 0; i < userIds.count; i ++) {
  93. NSString *userId = userIds[i];
  94. NSString *userName = [JXUserObject getUserNameWithUserId:userId];
  95. if (i == 0) {
  96. [userNameStr appendFormat:@"%@", userName];
  97. }else {
  98. [userNameStr appendFormat:@", %@", userName];
  99. }
  100. }
  101. JXSelectLabelGroupCell *cell=nil;
  102. NSString* cellName = @"labelCell";
  103. cell = [tableView dequeueReusableCellWithIdentifier:cellName];
  104. if(cell==nil){
  105. cell = [[JXSelectLabelGroupCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
  106. [_table addToPool:cell];
  107. }
  108. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  109. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%ld)",label.groupName, userIds.count];
  110. cell.textLabel.textColor = [UIColor blackColor];
  111. cell.textLabel.font = [UIFont systemFontOfSize:16.0];
  112. cell.detailTextLabel.text = userNameStr;
  113. cell.detailTextLabel.textColor = [UIColor grayColor];
  114. cell.detailTextLabel.font= [UIFont systemFontOfSize:15.0];
  115. BOOL flag = NO;
  116. for (NSInteger i = 0; i < _selLabels.count; i ++) {
  117. JXLabelObject *selLabelObj = _selLabels[i];
  118. if ([selLabelObj.groupId isEqualToString:label.groupId]) {
  119. flag = YES;
  120. break;
  121. }
  122. }
  123. QCheckBox* btn = [[QCheckBox alloc] initWithDelegate:self];
  124. btn.tag = indexPath.row;
  125. btn.frame = CGRectMake(15, 17, 25, 25);
  126. btn.selected = flag;
  127. [cell addSubview:btn];
  128. [_checkBoxArr addObject:btn];
  129. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - LINE_WH, JX_SCREEN_WIDTH, LINE_WH)];
  130. view.backgroundColor = THE_LINE_COLOR;
  131. [cell addSubview:view];
  132. return cell;
  133. }
  134. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  135. {
  136. return HEIGHT;
  137. }
  138. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  139. QCheckBox *checkBox = nil;
  140. for (NSInteger i = 0; i < _checkBoxArr.count; i ++) {
  141. QCheckBox *btn = _checkBoxArr[i];
  142. if (btn.tag == indexPath.row) {
  143. checkBox = btn;
  144. break;
  145. }
  146. }
  147. checkBox.selected = !checkBox.selected;
  148. [self didSelectedCheckBox:checkBox checked:checkBox.selected];
  149. }
  150. - (void)didReceiveMemoryWarning {
  151. [super didReceiveMemoryWarning];
  152. // Dispose of any resources that can be recreated.
  153. }
  154. /*
  155. #pragma mark - Navigation
  156. // In a storyboard-based application, you will often want to do a little preparation before navigation
  157. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  158. // Get the new view controller using [segue destinationViewController].
  159. // Pass the selected object to the new view controller.
  160. }
  161. */
  162. @end