// // ChoosePersonAddressVC.m // watch // // Created by WeiChaoZheng on 2017/10/28. // Copyright © 2017年 xTT. All rights reserved. // #import "ChoosePersonAddressVC.h" #import "PersonEditAddressCell.h" #import "ChooseContactCell.h" #import "CommonPopView.h" @interface ChoosePersonAddressVC () @property (strong,nonatomic) NSMutableDictionary *dataSouce; @property (strong,nonatomic) NSMutableArray *nameKeys; @end @implementation ChoosePersonAddressVC - (Contacts *)infoContacts{ if (!_infoContacts) { _infoContacts = [[Contacts alloc] init]; [_infoContacts setDefaultValue]; } return _infoContacts; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self initData]; self.zx_navTitle = @"添加联系人"; //设置 右上角的保存按钮 [self zx_setRightBtnWithText:@"提交" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self saveAction]; }]; self.zx_navRightBtn.enabled = NO; [self.zx_navRightBtn setTitleColor:RGB(255, 175, 141) forState:0]; [UICommon MessageUpload:@"正在加载"]; } -(void)saveAction { BOOL temp = false; for (NSString * key in self.dataSouce) { NSArray*arr = self.dataSouce[key]; for (PPPersonModel *model in arr) { if(model.isSelect){ temp = true; } } } if(!temp){ // LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:@"请至少选择一个要提交的联系人" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { // // } cancelHandler:nil destructiveHandler:nil]; // [alertView showAnimated:YES completionHandler:nil]; [UICommon MessageErrorText:@"请至少选择一个要提交的联系人"]; return; } CommonPopView *popView = [CommonPopView new]; [popView subPopView:popViewType_tjlxr PopTitle:@"提示" PopText:@"确认提交当前所选联系人?" okTouchBlock:^{ [self upPersonAddressData]; }]; // LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:@"确认提交当前所选联系人?" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"容我想想" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { // if(index == 0){ // [self upPersonAddressData]; // } // } cancelHandler:nil destructiveHandler:nil]; // [alertView showAnimated:YES completionHandler:nil]; } -(void)upPersonAddressData { // 保存数据,刷新TabelView // 遍历联系人的Model NSMutableArray *newArr = [NSMutableArray array]; for (NSString * key in self.dataSouce) { NSArray*arr = self.dataSouce[key]; for (PPPersonModel *model in arr) { if(model.isSelect){ // MARK: 添加短号 lsz for (NSString *mobile in model.mobileArray) { [newArr addObject:@{@"name":model.name,@"phone":mobile}]; } //[newArr addObject:@{@"name":model.name,@"phone":model.mobileArray[0]}]; } } } //MARK: 上传数据 [self.infoContacts.contact_arry addObjectsFromArray:[newArr copy]]; NSLog(@"@contact_arry = %zd", self.infoContacts.contact_arry.count); NSLog(@"@newArr = %zd", newArr.count); WEAKSELF [self.infoContacts saveManyPerson:^{ [weakSelf goBack]; } failure:^{ [self.infoContacts.contact_arry removeAllObjects]; }]; } -(void)goBack{ [self.navigationController popViewControllerAnimated:YES]; } -(void) initData{ WeakSelf //获取按联系人姓名首字拼音A~Z排序(已经对姓名的第二个字做了处理) [PPGetAddressBook getOrderAddressBook:^(NSDictionary *addressBookDict, NSArray *nameKeys) { //addressBookDict: 装着所有联系人的字典 //nameKeys: A~Z拼音字母数组; weakSelf.nameKeys = [NSMutableArray arrayWithArray:nameKeys]; weakSelf.dataSouce = [NSMutableDictionary dictionaryWithDictionary:addressBookDict]; //刷新 tableView [weakSelf initTabelView]; [UICommon HidenLoading]; } authorizationFailure:^{ LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:[NSString stringWithFormat:@"请为%@打开通讯录权限",APPName] message:@"" style:LGAlertViewStyleAlert buttonTitles:@[@"设置"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if([[UIApplication sharedApplication] canOpenURL:url]) { NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:url]; } } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; }]; } -(void) initTabelView { self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, iPhoneX_NavHeight, ScreenWidth, ScreenHeight-iPhoneX_NavHeight) style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.emptyDataSetSource = self; self.tableView.emptyDataSetDelegate = self; self.tableView.sectionIndexColor = mainColor; /// 索引文字颜色 self.emptyText = @"通讯录暂无联系人"; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubview:_tableView]; [_tableView registerNib:[UINib nibWithNibName:@"ChooseContactCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ChooseContactCellID"]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray *arr = self.dataSouce[self.nameKeys[section]]; return arr.count; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.nameKeys.count; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return self.nameKeys[section]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ChooseContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseContactCellID" forIndexPath:indexPath]; NSArray *arr = self.dataSouce[self.nameKeys[indexPath.section]]; PPPersonModel * model = arr[indexPath.row]; cell.nameLabel.text = model.name; // if(model.mobileArray.count > 1){ // cell.phoneLabel.text = [NSString stringWithFormat:@"%@(%zd)",model.mobileArray[0],model.mobileArray.count]; // } else if (model.mobileArray.count > 0) { // cell.phoneLabel.text = model.mobileArray[0]; // } else{ // cell.phoneLabel.text = @"未知"; // } if (model.isSelect) { cell.selectImageView.image = [UIImage imageNamed:@"选中"]; }else{ cell.selectImageView.image = [UIImage imageNamed:@"未选中"]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 56; } -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return self.nameKeys; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // ChoosePACell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChoosePACellID" forIndexPath:indexPath]; NSMutableArray *Marr = [NSMutableArray arrayWithArray:self.dataSouce[self.nameKeys[indexPath.section]]]; PPPersonModel * model = Marr[indexPath.row]; if (model.mobileArray.count == 0) { LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"添加的通讯录号码不能为空" message:@"" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; return; } model.isSelect = !model.isSelect; [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; BOOL temp = false; for (NSString * key in self.dataSouce) { NSArray*arr = self.dataSouce[key]; for (PPPersonModel *model in arr) { if(model.isSelect){ temp = true; } } } self.zx_navRightBtn.enabled = temp ? YES : NO; [self.zx_navRightBtn setTitleColor:temp ? mainColor : RGB(255, 175, 141) forState:0]; } @end