JXSelectAddressBookVC.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // JXSelectAddressBookVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2019/4/3.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXSelectAddressBookVC.h"
  9. #import "QCheckBox.h"
  10. #import "BMChineseSort.h"
  11. #import "JXAddressBookCell.h"
  12. @interface JXSelectAddressBookVC ()<QCheckBoxDelegate,JXAddressBookCellDelegate>
  13. @property(nonatomic,strong)NSMutableArray *array;
  14. //排序后的出现过的拼音首字母数组
  15. @property(nonatomic,strong)NSMutableArray *indexArray;
  16. //排序好的结果数组
  17. @property(nonatomic,strong)NSMutableArray *letterResultArr;
  18. @property (nonatomic, strong)NSMutableArray *abUreadArr;
  19. @property (nonatomic, assign) BOOL isShowSelect;
  20. @property (nonatomic, strong) NSMutableArray *selectABs;
  21. @property (nonatomic, strong) UIView *doneBtn;
  22. @property (nonatomic, strong) UIButton *selectBtn;
  23. @property (nonatomic, strong) NSDictionary *phoneNumDict;
  24. @property (nonatomic, strong) NSMutableArray *headerCheckBoxs;
  25. @property (nonatomic, strong) NSMutableArray *allAbArr;
  26. @end
  27. @implementation JXSelectAddressBookVC
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. // Do any additional setup after loading the view.
  31. self.heightHeader = JX_SCREEN_TOP;
  32. self.heightFooter = 0;
  33. self.isGotoBack = YES;
  34. //self.view.frame = g_window.bounds;
  35. self.isShowFooterPull = NO;
  36. [self createHeadAndFoot];
  37. _phoneNumDict = [[JXAddressBook sharedInstance] getMyAddressBook];
  38. _headerCheckBoxs = [NSMutableArray array];
  39. self.title = Localized(@"SELECT_CONTANTS");
  40. _array = [NSMutableArray array];
  41. _indexArray = [NSMutableArray array];
  42. _letterResultArr = [NSMutableArray array];
  43. _selectABs = [NSMutableArray array];
  44. _allAbArr = [NSMutableArray array];
  45. _selectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  46. [_selectBtn setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  47. [_selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  48. _selectBtn.tintColor = [UIColor clearColor];
  49. _selectBtn.frame = CGRectMake(JX_SCREEN_WIDTH - 32-15, JX_SCREEN_TOP - 30, 32, 15);
  50. _selectBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
  51. [_selectBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  52. [self.tableHeader addSubview:_selectBtn];
  53. [self getServerData];
  54. }
  55. - (void)didSelectedCheckBox:(QCheckBox *)checkbox checked:(BOOL)checked{
  56. [self addressBookCell:nil checkBoxSelectIndexNum:checkbox.tag isSelect:checked];
  57. }
  58. - (void)selectBtnAction:(UIButton *)btn {
  59. if (_selectABs.count <= 0) {
  60. [g_App showAlert:Localized(@"JX_PleaseSelectContactPerson")];
  61. return;
  62. }
  63. if ([self.delegate respondsToSelector:@selector(selectAddressBookVC:doneAction:)]) {
  64. [self.delegate selectAddressBookVC:self doneAction:_selectABs];
  65. }
  66. [self actionQuit];
  67. }
  68. - (void)getServerData {
  69. [_array removeAllObjects];
  70. [_allAbArr removeAllObjects];
  71. NSMutableArray *arr = [[JXAddressBook sharedInstance] fetchAllAddressBook];
  72. for (NSString *key in _phoneNumDict.allKeys) {
  73. BOOL flag = NO;
  74. for (JXAddressBook *obj in arr) {
  75. if ([obj.toTelephone isEqualToString:key]) {
  76. flag = YES;
  77. break;
  78. }
  79. }
  80. if (!flag) {
  81. JXAddressBook *ab = [[JXAddressBook alloc] init];
  82. ab.toTelephone = key;
  83. ab.addressBookName = _phoneNumDict[key];
  84. [_array addObject:ab];
  85. }
  86. }
  87. //选择拼音 转换的 方法
  88. BMChineseSortSetting.share.sortMode = 2; // 1或2
  89. //排序 Person对象
  90. [BMChineseSort sortAndGroup:_array key:@"addressBookName" finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr) {
  91. if (isSuccess) {
  92. self.indexArray = sectionTitleArr;
  93. self.letterResultArr = sortedObjArr;
  94. [_table reloadData];
  95. }
  96. }];
  97. // //根据Person对象的 name 属性 按中文 对 Person数组 排序
  98. // self.indexArray = [BMChineseSort IndexWithArray:_array Key:@"addressBookName"];
  99. // self.letterResultArr = [BMChineseSort sortObjectArray:_array Key:@"addressBookName"];
  100. }
  101. #pragma mark ---------tableView协议----------------
  102. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  103. {
  104. return [self.indexArray count];
  105. }
  106. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  107. return [self.indexArray objectAtIndex:section];
  108. }
  109. -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
  110. UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
  111. header.tintColor = HEXCOLOR(0xF2F2F2);
  112. [header.textLabel setTextColor:HEXCOLOR(0x999999)];
  113. [header.textLabel setFont:SYSFONT(15)];
  114. }
  115. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  116. {
  117. return [(NSArray *)[self.letterResultArr objectAtIndex:section] count];
  118. }
  119. -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
  120. return self.indexArray;
  121. }
  122. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
  123. return index;
  124. }
  125. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. NSString *identifier = @"JXAddressBookCell";
  128. JXAddressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  129. JXAddressBook *addressBook = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  130. if (!cell) {
  131. cell = [[JXAddressBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  132. }
  133. cell.isInvite = YES;
  134. cell.index = indexPath.section * 1000 + indexPath.row;
  135. cell.isShowSelect = YES;
  136. cell.delegate = self;
  137. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  138. if ([_selectABs containsObject:addressBook]) {
  139. cell.checkBox.selected = YES;
  140. }else {
  141. cell.checkBox.selected = NO;
  142. }
  143. cell.addressBook = addressBook;
  144. cell.headImage.userInteractionEnabled = NO;
  145. if (indexPath.row == [(NSArray *)[self.letterResultArr objectAtIndex:indexPath.section] count]-1) {
  146. cell.lineView.frame = CGRectMake(cell.lineView.frame.origin.x, cell.lineView.frame.origin.y, cell.lineView.frame.size.width,0);
  147. }else {
  148. cell.lineView.frame = CGRectMake(cell.lineView.frame.origin.x, cell.lineView.frame.origin.y, cell.lineView.frame.size.width,LINE_WH);
  149. }
  150. return cell;
  151. }
  152. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  153. return 64;
  154. }
  155. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157. JXAddressBookCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  158. cell.checkBox.selected = !cell.checkBox.selected;
  159. [self addressBookCell:cell checkBoxSelectIndexNum:cell.index isSelect:cell.checkBox.selected];
  160. }
  161. - (void)addressBookCell:(JXAddressBookCell *)abCell checkBoxSelectIndexNum:(NSInteger)indexNum isSelect:(BOOL)isSelect {
  162. JXAddressBook *ab;
  163. if (abCell) {
  164. ab = [[self.letterResultArr objectAtIndex:abCell.index / 1000] objectAtIndex:abCell.index % 1000];
  165. }else {
  166. ab = self.abUreadArr[indexNum];
  167. }
  168. if (isSelect) {
  169. [_selectABs addObject:ab];
  170. }else {
  171. [_selectABs removeObject:ab];
  172. }
  173. }
  174. /*
  175. #pragma mark - Navigation
  176. // In a storyboard-based application, you will often want to do a little preparation before navigation
  177. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  178. // Get the new view controller using [segue destinationViewController].
  179. // Pass the selected object to the new view controller.
  180. }
  181. */
  182. @end