// // 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]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self zx_setRightBtnWithText:@"保存" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self rightBarItemClick]; }]; [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.view).offset(iPhoneX_NavHeight); make.bottom.equalTo(self.view.mas_bottom); }]; //注册 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){ [SVProgressHUD showErrorWithStatus:@"请设置地址名称"]; return; } if(self.infoRail.address == nil || self.infoRail.address == 0){ [SVProgressHUD showErrorWithStatus:@"请设置地址"]; return; } [self.infoRail saveSuccess:^{ [self goBack:nil]; } failure:^{}]; } - (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 = @""; cell.textLabel.text = title; cell.textLabel.font = DefineFontSize; [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.textLabel.text = title; cell.textLabel.font = DefineFontSize; cell.textLabel.backgroundColor = [UIColor clearColor]; cell.aTitleLabel.text = @""; 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:UITableViewCellStyleValue1 reuseIdentifier:@"TextFieldCell"]; } cell.textLabel.text = title; cell.textField.font = DefineFontSize; if([title isEqualToString:@"地址名称"]){ 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; // } // }; }else if([title isEqualToString:@"地址"]){ 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.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