Contacts_VC.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // Contacts_VC.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2019/1/5.
  6. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "Contacts_VC.h"
  9. #import "CustomAlertView.h"
  10. #import "Header.h"
  11. #import "PersonCell.h"
  12. #import <AddressBookUI/ABPeoplePickerNavigationController.h>
  13. #import <AddressBook/ABPerson.h>
  14. #import <AddressBookUI/ABPersonViewController.h>
  15. @interface Contacts_VC ()<UITableViewDelegate,UITableViewDataSource,ABPeoplePickerNavigationControllerDelegate,UITextFieldDelegate>
  16. @property (nonatomic,strong)UIView *headerView;
  17. @property (nonatomic,strong)UITableView *myTable;
  18. @property (nonatomic,strong)UITextField *textName;
  19. @property (nonatomic,strong)UITextField *textPhone;
  20. @property (nonatomic,strong)NSMutableArray *dataArray;
  21. @property (nonatomic,assign)NSInteger selectedId;
  22. @end
  23. @implementation Contacts_VC
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view.
  27. self.view.backgroundColor = [UIColor whiteColor];
  28. self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1];
  29. [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
  30. //设置导航栏左侧按钮
  31. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  32. [backBtn setImage:[UIImage imageNamed:@"hsh_return"] forState:UIControlStateNormal];
  33. backBtn.frame = CGRectMake(0, 0, 44, 44);
  34. [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  35. backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  36. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
  37. self.navigationItem.title =@"联系人";
  38. [self setupUI];
  39. //获取联系人 为null报错
  40. NSLog(@"这是联系人要穿的场所ID%@",_SiteId);
  41. [DataSourceManager getlinkmanWithUrlSiteId:_SiteId completionBlock:^(NSMutableArray *array) {
  42. _dataArray =array;
  43. NSLog(@"%@---->",array);
  44. [_myTable reloadData];
  45. }];
  46. //
  47. //集成下拉刷新控件
  48. [self setupDownRefresh];
  49. }
  50. -(void)backClick{
  51. [self.navigationController popViewControllerAnimated:YES];
  52. }
  53. /**
  54. * 集成下拉刷新控件
  55. */
  56. - (void)setupDownRefresh
  57. {
  58. // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
  59. self.myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewDatas)];
  60. //马上进入刷新状态
  61. [self.myTable.mj_header beginRefreshing];
  62. }
  63. -(void)loadNewDatas{
  64. _pageIndex =1;//默认加载第一页
  65. [self.dataArray removeAllObjects];
  66. //请求数据
  67. [DataSourceManager getlinkmanWithUrlSiteId:_SiteId completionBlock:^(NSMutableArray *array) {
  68. _dataArray =array;
  69. [_myTable reloadData];
  70. //结束刷新
  71. [self.myTable.mj_header endRefreshing];
  72. }];
  73. [self.myTable.mj_header endRefreshing];
  74. }
  75. //取消选择
  76. - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
  77. {
  78. [peoplePicker dismissViewControllerAnimated:YES completion:nil];
  79. }
  80. -(void)setupUI{
  81. UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
  82. rightButton.frame = CGRectMake(0, 0, 40, 40);
  83. [rightButton setImage:[UIImage imageNamed:@"hsh_home_add_white"] forState:UIControlStateNormal];
  84. [rightButton addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
  85. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
  86. self.navigationItem.rightBarButtonItem = rightItem;
  87. _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
  88. _myTable.delegate = self;
  89. _myTable.dataSource = self;
  90. [self.view addSubview:_myTable];
  91. _myTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  92. }
  93. #pragma mark--EVENT--------
  94. -(void)Click{
  95. [[CustomAlertView sharedAlertView] showAlertViewWithConfirmAction:^(NSArray *inputText) {
  96. NSLog(@"输入内容:%@", inputText);
  97. NSString *name = [inputText objectAtIndex:0];
  98. NSString *phone = [inputText objectAtIndex:1];
  99. [DataSourceManager addlinkmanWithUrlSiteId:_SiteId Name:name Phone:phone completionBlock:^(NSDictionary *dic) {
  100. NSLog(@"这是联系人%@",dic);
  101. [DataSourceManager getlinkmanWithUrlSiteId:_SiteId completionBlock:^(NSMutableArray *array) {
  102. _dataArray =array;
  103. [_myTable reloadData];
  104. //结束刷新
  105. [self.myTable.mj_header endRefreshing];
  106. }];
  107. [self.myTable.mj_header endRefreshing];
  108. }];
  109. } andReloadAction:^{
  110. }];
  111. }
  112. -(void)popView{
  113. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"添加联系人" message:nil preferredStyle:UIAlertControllerStyleAlert];
  114. [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  115. [self dismissViewControllerAnimated:YES completion:nil];
  116. }]];
  117. [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  118. UITextField *nameTF = alertController.textFields.firstObject;
  119. UITextField *phoneTF = alertController.textFields.lastObject;
  120. }]];
  121. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  122. textField.delegate = self;
  123. textField.placeholder = @"请输入联系人姓名:";
  124. }];
  125. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  126. textField.delegate =self;
  127. textField.placeholder = @"请输入联系人手机号码:";
  128. textField.keyboardType = UIKeyboardTypeNumberPad;
  129. }];
  130. // [alertController addAction:[UIAlertAction actionWithTitle:@"访问通讯录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  131. // NSLog(@"访问通讯录");
  132. // //初始化通讯录
  133. // ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc] init];
  134. // nav.peoplePickerDelegate = self;
  135. // // if(IOS8_OR_LATER){
  136. // // nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
  137. // // }
  138. // [self presentViewController:nav animated:YES completion:nil];
  139. //
  140. // }]];
  141. [self presentViewController:alertController animated:YES completion:nil];
  142. }
  143. #pragma mark----tableviewDelegate
  144. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  145. return _dataArray.count;
  146. }
  147. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  148. return 44;
  149. }
  150. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  151. static NSString *cellID = @"cell";
  152. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];;
  153. if (!cell) {
  154. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  155. }
  156. contactModel *contact = [self.dataArray objectAtIndex:indexPath.row];
  157. cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",contact.name,contact.phone];
  158. _selectedId = indexPath.row;
  159. cell.preservesSuperviewLayoutMargins = false;
  160. //添加删除按钮
  161. UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  162. deleteBtn.frame = CGRectMake(WIDTH-54, 0, 44, 44);
  163. [deleteBtn setImage:[UIImage imageNamed:@"hsh_login_delete"] forState:UIControlStateNormal];
  164. [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  165. [cell.contentView addSubview:deleteBtn];
  166. return cell;
  167. }
  168. //编辑单元格使用的方法
  169. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  170. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  171. }
  172. -(void)deleteBtnClick:(UIButton *)btn{
  173. contactModel *contact = [self.dataArray objectAtIndex:_selectedId];
  174. NSString *contactId = [NSString stringWithFormat:@"%@",contact.ID];
  175. [DataSourceManager deletelinkmanWithUrlId:contactId completionBlock:^(NSDictionary *dic) {
  176. //
  177. [self.dataArray removeObjectAtIndex:_selectedId];
  178. ALERTSHOW(@"删除成功");
  179. [_myTable reloadData];
  180. }];
  181. }
  182. -(BOOL)textFieldShouldReturn:(UITextField *)textField{
  183. [textField resignFirstResponder];
  184. return YES;
  185. }
  186. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  187. [self.view endEditing:YES];
  188. }
  189. - (void)didReceiveMemoryWarning {
  190. [super didReceiveMemoryWarning];
  191. // Dispose of any resources that can be recreated.
  192. }
  193. -(void)viewWillDisappear:(BOOL)animated{
  194. NSLog(@"----%@",_siteStr);
  195. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_siteStr,@"site", nil];
  196. [[NSNotificationCenter defaultCenter]postNotificationName:@"changeValue" object:nil userInfo:dic];
  197. }
  198. /*
  199. #pragma mark - Navigation
  200. // In a storyboard-based application, you will often want to do a little preparation before navigation
  201. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  202. // Get the new view controller using [segue destinationViewController].
  203. // Pass the selected object to the new view controller.
  204. }
  205. */
  206. @end