// // YXAddFamilyViewController.m // Temperature // // Created by TC on 2025/3/23. // #import "YXAddFamilyViewController.h" #import "YXAddressViewController.h" #import "YXDefaultRoomModel.h" #import "RDSRootControl.h" @interface YXAddFamilyViewController () @property (weak, nonatomic) UIView *contentView; @property (weak, nonatomic) UITableView *tableView; @property (weak, nonatomic) UITextField *nameTextField; @property (weak, nonatomic) UILabel *adTextLabel; @property (strong, nonatomic) NSMutableArray *familyArray; @property (nonatomic,copy) NSString *province; @property (nonatomic,copy) NSString *city; @property (nonatomic,copy) NSString *area; @property (nonatomic,copy) NSString *address; @end @implementation YXAddFamilyViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; self.navigationItem.title = @"新建家庭"; self.familyArray = [[NSMutableArray alloc]initWithCapacity:5]; [self createTableView]; [self getDefaultRooms]; } -(void)getDefaultRooms { RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_getDefaultRoomsSuccess:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { NSDictionary *dataDic = responseObject[@"data"]; NSArray *list = dataDic[@"list"]; weakSelf.familyArray = [YXDefaultRoomModel mj_objectArrayWithKeyValuesArray:list]; [weakSelf.tableView reloadData]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)createTableView { UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped]; tableView.delegate = self; tableView.dataSource = self; tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"addFamilyCellID"]; [self.view addSubview:tableView]; self.tableView = tableView; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.right.equalTo(@-15); make.left.equalTo(@15); make.bottom.equalTo(@(-kSafeBottomHeight)); }]; tableView.layer.cornerRadius = 8; tableView.clipsToBounds = YES; UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [saveBtn setTitle:@"保存" forState:UIControlStateNormal]; [saveBtn addTarget:self action:@selector(saveBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [saveBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal]; saveBtn.titleLabel.font = [UIFont systemFontOfSize:16]; saveBtn.backgroundColor = [UIColor colorWithHexString:@"#267AFF"]; [self.view addSubview:saveBtn]; [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@45); make.left.equalTo(@20); make.right.equalTo(@-20); make.bottom.equalTo(@(-kSafeBottomHeight)); }]; saveBtn.layer.cornerRadius = 22.5; [self createTableHeaderView]; [self.tableView setEditing:YES animated:YES]; } -(void)saveBtnAction:(UIButton *)btn { if (self.nameTextField.text.length == 0) { [RDSHudShower showBottomToast:@"家庭名称不能为空"]; return; } NSMutableArray *roomsArray = [[NSMutableArray alloc]initWithCapacity:5]; for (YXDefaultRoomModel *model in self.familyArray) { [roomsArray addObject:model.name]; } RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_addHomeWithName:self.nameTextField.text province:self.province city:self.city district:self.area address:self.address rooms:roomsArray success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower showBottomToast:@"创建成功"]; [weakSelf popViewControllerAnimated:YES]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)createTableHeaderView { UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 180)]; headerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; UIView *bgView = [[UIView alloc]init]; bgView.backgroundColor = [UIColor whiteColor]; [headerView addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@15); make.right.equalTo(@0); make.left.equalTo(@0); make.height.equalTo(@110); }]; bgView.layer.cornerRadius = 8; UILabel *nameLabel = [[UILabel alloc]init]; nameLabel.textColor = [UIColor colorWithHexString:@"#333333"]; nameLabel.font = [UIFont systemFontOfSize:15]; nameLabel.text = @"家庭名称"; nameLabel.font = [UIFont boldSystemFontOfSize:15]; [bgView addSubview:nameLabel]; [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@0); make.left.equalTo(@15); make.height.equalTo(@55); make.width.equalTo(@80); }]; UITextField *nameTextField = [[UITextField alloc]init]; nameTextField.placeholder = @"请输入家庭名称(20字以内)"; nameTextField.font = [UIFont systemFontOfSize:15]; nameTextField.textAlignment = NSTextAlignmentRight; [bgView addSubview:nameTextField]; [nameTextField mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@0); make.right.equalTo(@-15); make.height.equalTo(@55); make.left.equalTo(nameLabel.mas_right).offset(5); }]; self.nameTextField = nameTextField; UIView *lineView = [[UIView alloc]init]; lineView.backgroundColor = [UIColor colorWithHexString:@"#F2F5FA"]; [bgView addSubview:lineView]; [lineView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(nameLabel.mas_bottom); make.right.equalTo(@-15); make.left.equalTo(@15); make.height.equalTo(@1); }]; UILabel *adNameLabel = [[UILabel alloc]init]; adNameLabel.textColor = [UIColor colorWithHexString:@"#333333"]; adNameLabel.font = [UIFont systemFontOfSize:15]; adNameLabel.text = @"家庭地址"; adNameLabel.font = [UIFont boldSystemFontOfSize:15]; [bgView addSubview:adNameLabel]; [adNameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lineView.mas_bottom); make.left.equalTo(@15); make.height.equalTo(@55); make.width.equalTo(@80); }]; UIImageView *imgView = [[UIImageView alloc]init]; imgView.image = [UIImage imageNamed:@"home_right_arrow"]; [bgView addSubview:imgView]; [imgView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(adNameLabel.mas_centerY); make.right.equalTo(@-15); make.width.equalTo(@9); make.height.equalTo(@15); }]; UILabel *adTextLabel = [[UILabel alloc]init]; adTextLabel.textColor = [UIColor colorWithHexString:@"#999999"]; adTextLabel.font = [UIFont systemFontOfSize:15]; adTextLabel.text = @"请选择地址"; adTextLabel.textAlignment = NSTextAlignmentRight; [bgView addSubview:adTextLabel]; [adTextLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lineView.mas_bottom); make.right.equalTo(imgView.mas_left).offset(-10); make.height.equalTo(@55); make.left.equalTo(adNameLabel.mas_right).offset(5); }]; adTextLabel.userInteractionEnabled = YES; self.adTextLabel = adTextLabel; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toAddressAction)]; [adTextLabel addGestureRecognizer:tap]; UILabel *listLabel = [[UILabel alloc]init]; listLabel.textColor = [UIColor colorWithHexString:@"#666666"]; listLabel.font = [UIFont systemFontOfSize:15]; listLabel.text = @"房间列表"; [headerView addSubview:listLabel]; [listLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgView.mas_bottom); make.left.equalTo(@15); make.height.equalTo(@55); make.width.equalTo(@150); }]; UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [addBtn setTitle:@"新增房间" forState:UIControlStateNormal]; [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [addBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal]; addBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [headerView addSubview:addBtn]; [addBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgView.mas_bottom); make.right.equalTo(@-15); make.height.equalTo(@55); make.width.equalTo(@80); }]; self.tableView.tableHeaderView = headerView; } -(void)addBtnAction:(UIButton *)btn { [self alertWithText:@"房间名称"]; } -(void)alertWithText:(NSString *)text { RDS_WEAKSELF(weakSelf) UIAlertController *alertController = [UIAlertController alertControllerWithTitle:text message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *textFeild = [alertController.textFields objectAtIndex:0]; NSLog(@"%@",textFeild.text); YXDefaultRoomModel *newModel = [[YXDefaultRoomModel alloc]init]; newModel.name = textFeild.text; [weakSelf.familyArray addObject:newModel]; [weakSelf.tableView reloadData]; }]; [alertController addAction:sureAction]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:cancelAction]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = [NSString stringWithFormat:@"请输入%@",text]; }]; [self presentViewController:alertController animated:YES completion:nil]; } -(void)toAddressAction { RDS_WEAKSELF(weakSelf) YXAddressViewController *vc = [[YXAddressViewController alloc]init]; [self.navigationController pushViewController:vc animated:YES]; vc.sureBtnClick = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull area, NSString * _Nonnull address) { weakSelf.province = province; weakSelf.city = city; weakSelf.area = area; weakSelf.address = address; weakSelf.adTextLabel.text = [NSString stringWithFormat:@"%@%@%@%@",province,city,area,address]; }; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.familyArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addFamilyCellID" forIndexPath:indexPath]; YXDefaultRoomModel *model = self.familyArray[indexPath.row]; cell.textLabel.text = model.name; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc]init]; return headerView; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc]init]; return footerView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.01; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 65; } #pragma mark - edit -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.familyArray removeObjectAtIndex:indexPath.row]; [tableView reloadData]; } } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end