// // AddRailViewController.m // tongxin // // Created by WeiChaoZheng on 2018/8/27. // Copyright © 2018年 xTT. All rights reserved. // #import "AddRailViewController.h" #import "RailAddressViewController.h" #import "RailADNameChooseVC.h" @interface AddRailViewController () @end @implementation AddRailViewController @synthesize myDataSource = _myDataSource; - (NSMutableArray *)myDataSource{ if (!_myDataSource) { _myDataSource = [[NSMutableArray alloc] initWithArray:@[@[@"地址名称",@"地址"],@[@"有效时间",@"星期"]]]; } return _myDataSource; } - (Rail *)infoRail{ if (!_infoRail) { _infoRail = [[Rail alloc] init]; [_infoRail setDefaultValue]; } return _infoRail; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; if (!self.isAdd) { self.title = @"编辑电子围栏"; }else{ self.title = @"添加电子围栏"; } [self.myTableView reloadData]; if((self.infoRail.name != nil || self.infoRail.name.length > 0) && (self.infoRail.address != nil || self.infoRail.address > 0)) { self.zx_navRightBtn.alpha = 1; self.zx_navRightBtn.enabled = YES; } else { self.zx_navRightBtn.alpha = .5; self.zx_navRightBtn.enabled = NO; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self zx_setRightBtnWithText:@"保存" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self rightBarItemClick]; }]; self.zx_navRightBtn.enabled = NO; self.zx_backBtnImageName = @""; [self zx_setLeftBtnWithText:@"取消" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self.navigationController popViewControllerAnimated:YES]; }]; [self.zx_navRightBtn setTitleColor:mainColor forState:0]; self.myTableView.frame = CGRectMake(15, iPhoneX_NavHeight, ScreenWidth-30, ScreenHeight-iPhoneX_NavHeight); self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone; //注册 cell [self.myTableView registerNib:[UINib nibWithNibName:@"AddWeekTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AddWeekTableViewCellID"]; [self.myTableView registerNib:[UINib nibWithNibName:@"DurationTimeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"DurationTimeTableViewCellID"]; } - (void)rightBarItemClick { [self.view endEditing:YES]; //比较2个时间段 结束时间需大于开始时间 NSDateFormatter * df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"HH:mm"]; double startTime =[df dateFromString:self.infoRail.starttime].timeIntervalSince1970; double endTime =[df dateFromString:self.infoRail.endedtime].timeIntervalSince1970; if(endTime <= startTime){ 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; } self.infoRail.status = @(1); if(self.infoRail.name == nil || self.infoRail.name.length == 0){ [UICommon MessageErrorText:@"请设置地址名称"]; return; } if(self.infoRail.address == nil || self.infoRail.address == 0){ [UICommon MessageErrorText:@"请设置地址"]; return; } [self.infoRail saveSuccess:^{ [self goBack:nil]; } failure:^{}]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.myDataSource.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.myDataSource[section] count]; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 12; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return indexPath.section == 0 ? 55 : 112; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; if ([title isEqualToString:@"星期"]) { AddWeekTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddWeekTableViewCellID" forIndexPath:indexPath]; cell.aTitleLabel.text = title; [cell setWeek:self.infoRail.week]; WEAKSELF cell.weekSelectBlack = ^(NSString *selectWeek) { weakSelf.infoRail.week = selectWeek; }; cell.accessoryType = UITableViewCellAccessoryNone; return cell; } else if([title isEqualToString:@"有效时间"]) { DurationTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DurationTimeTableViewCellID" forIndexPath:indexPath]; cell.aTitleLabel.text = title; cell.startTextField.text = self.infoRail.starttime; cell.endTextField.text = self.infoRail.endedtime; NSDateFormatter * df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"HH:mm"]; [cell timeInputViewWithModel:UIDatePickerModeTime WithTextField:cell.startTextField WithDate:[df dateFromString:self.infoRail.starttime]]; [cell timeInputViewWithModel:UIDatePickerModeTime WithTextField:cell.endTextField WithDate:[df dateFromString:self.infoRail.endedtime]]; WEAKSELF __block DurationTimeTableViewCell *assCell = cell; cell.durationBlock = ^(NSString *textStr, BOOL isStart) { if (isStart) { weakSelf.infoRail.starttime = textStr; //比较2个时间段 开始时间需小于结束时间 NSDateFormatter * df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"HH:mm"]; double startTime =[df dateFromString:weakSelf.infoRail.starttime].timeIntervalSince1970; double endTime =[df dateFromString:weakSelf.infoRail.endedtime].timeIntervalSince1970; if(startTime >= endTime ){ [assCell.startTextField resignFirstResponder]; LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:@"开始时间需小于结束时间" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { //焦点移动到 结束时间 [assCell.startTextField becomeFirstResponder]; } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; } } else { weakSelf.infoRail.endedtime = textStr; //比较2个时间段 结束时间需大于开始时间 NSDateFormatter * df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"HH:mm"]; double startTime =[df dateFromString:weakSelf.infoRail.starttime].timeIntervalSince1970; double endTime =[df dateFromString:weakSelf.infoRail.endedtime].timeIntervalSince1970; if(endTime <= startTime){ [assCell.endTextField resignFirstResponder]; LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:@"结束时间需大于开始时间" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { //焦点移动到 结束时间 [assCell.endTextField becomeFirstResponder]; } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; } } }; return cell; } else { TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell"]; if (!cell) { cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextFieldCell"]; } cell.textLabel.text = title; cell.textField.font = FontBold_(14); cell.textField.textColor = KKBlack20; UIImageView *rightImg = [UIImageView new]; rightImg.image = ImageName_(@"icon_enter_gray"); rightImg.frame = CGRectMake(0, 0, rightImg.image.size.width, rightImg.image.size.height); [cell setAccessoryView:rightImg]; if([title isEqualToString:@"地址名称"]) { [UICommon ui_uiViewFillet:CGRectMake(0, 0, ScreenWidth-30, 55) Viewdirection:PYUIdirectionTop toView:cell sizeMake:12]; cell.textField.textAlignment = NSTextAlignmentRight; cell.textField.text = self.infoRail.name; cell.textField.placeholder = @"请设置地址名称"; [cell.textField setEnabled:NO]; // WEAKSELF // cell.block = ^(id sender, TextFieldCell *blockCell) { // if([title isEqualToString:@"地址名称"]){ // weakSelf.infoRail.name = blockCell.textField.text; // } // }; UIImageView *line = [UIImageView new]; line.backgroundColor = RGB(240, 240, 240); [cell addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(cell.contentView).offset(16); make.right.equalTo(cell.contentView).offset(17); make.bottom.equalTo(cell.contentView.mas_bottom); make.height.mas_equalTo(0.5); }]; } else if([title isEqualToString:@"地址"]) { [UICommon ui_uiViewFillet:CGRectMake(0, 0, ScreenWidth-30, 55) Viewdirection:PYUIdirectionBotton toView:cell sizeMake:12]; cell.textField.textAlignment = NSTextAlignmentRight; cell.textField.text = self.infoRail.address; cell.textField.placeholder = @"请设置详细地址"; [cell.textField setEnabled:NO]; } return cell; } return nil; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; if (indexPath.section == 0) { if (indexPath.row == 0) { //地址名称 RailADNameChooseVC *vc = [sb instantiateViewControllerWithIdentifier:@"RailADNameChooseVC"]; vc.infoRail = self.infoRail; [self.navigationController pushViewController:vc animated:YES]; }else if(indexPath.row == 1){ //地址 RailAddressViewController *vc = [sb instantiateViewControllerWithIdentifier:@"RailAddressViewController"]; vc.infoRail = self.infoRail; [self.navigationController pushViewController:vc animated:YES]; } } } @end