|
|
@@ -0,0 +1,192 @@
|
|
|
+//
|
|
|
+// CommonEditorRangeCell.m
|
|
|
+// RedAnt Mobile
|
|
|
+//
|
|
|
+// Created by Jack on 2017/11/13.
|
|
|
+// Copyright © 2017年 Ray. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#import "CommonEditorRangeCell.h"
|
|
|
+#import "DatePickerViewController.h"
|
|
|
+
|
|
|
+
|
|
|
+@interface CommonEditorRangeCell ()<UITextFieldDelegate>
|
|
|
+
|
|
|
+@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
|
|
|
+@property (strong, nonatomic) IBOutlet UITextField *minField;
|
|
|
+@property (strong, nonatomic) IBOutlet UITextField *maxField;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+
|
|
|
+@implementation CommonEditorRangeCell
|
|
|
+
|
|
|
+- (void)awakeFromNib {
|
|
|
+ [super awakeFromNib];
|
|
|
+ // Initialization code
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
|
|
|
+ [super setSelected:selected animated:animated];
|
|
|
+
|
|
|
+ // Configure the view for the selected state
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Setter
|
|
|
+
|
|
|
+- (void)setKeyboardType:(UIKeyboardType)keyboardType {
|
|
|
+ self.minField.keyboardType = keyboardType;
|
|
|
+ self.maxField.keyboardType = keyboardType;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setType:(CommonEditorRangeType)type {
|
|
|
+ _type = type;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setMinValue:(NSString *)min maxValue:(NSString *)max {
|
|
|
+ self.minField.text = min;
|
|
|
+ self.maxField.text = max;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setTitle:(NSString *)title {
|
|
|
+ self.titleLabel.text = title;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - TextField Delegate
|
|
|
+
|
|
|
+- (BOOL)textFieldShouldReturn:(UITextField *)textField {
|
|
|
+ [textField resignFirstResponder];
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
|
|
|
+ if (self.type == CommonEditorRangeTypeDate) {
|
|
|
+ // 显示DatePicker
|
|
|
+ [self showDatePickerWithField:textField];
|
|
|
+ return NO;
|
|
|
+ }
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
|
|
|
+ // 判断min是否大于max
|
|
|
+ if (self.type == CommonEditorRangeTypeDate) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ NSString *max_str = self.maxField.text;
|
|
|
+ NSString *min_str = self.minField.text;
|
|
|
+ float min = 0;
|
|
|
+ float max = MAXFLOAT;
|
|
|
+
|
|
|
+ if (textField == self.minField) {
|
|
|
+ min = [min_str floatValue];
|
|
|
+ max_str = [max_str stringByReplacingOccurrencesOfString:@" " withString:@""];
|
|
|
+ if (max_str != nil && max_str.length > 1) {
|
|
|
+ max = [max_str floatValue];
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (textField == self.maxField) {
|
|
|
+ max = [max_str floatValue];
|
|
|
+ min_str = [min_str stringByReplacingOccurrencesOfString:@" " withString:@""];
|
|
|
+ if (min_str != nil && min_str.length > 1) {
|
|
|
+ min = [min_str floatValue];
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (min > max) {
|
|
|
+ [self showAlert];
|
|
|
+ return NO;
|
|
|
+ }
|
|
|
+ if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorRangeCell:didChangeMinValue:maxValue:atIndexPath:)]) {
|
|
|
+ [self.delegate commonEditorRangeCell:self didChangeMinValue:self.minField.text maxValue:self.maxField.text atIndexPath:self.indexPath];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Private
|
|
|
+
|
|
|
+- (UIViewController *)viewController {
|
|
|
+ for (UIView* next = [self superview]; next; next = next.superview) {
|
|
|
+ UIResponder *nextResponder = [next nextResponder];
|
|
|
+ if ([nextResponder isKindOfClass:[UIViewController class]]) {
|
|
|
+ return (UIViewController *)nextResponder;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)showAlert {
|
|
|
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"minimum can't larger than maximum" preferredStyle:UIAlertControllerStyleAlert];
|
|
|
+ UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:nil];
|
|
|
+ [alert addAction:action];
|
|
|
+ [[self viewController] presentViewController:alert animated:YES completion:nil];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)showDatePickerWithField:(UITextField *)field {
|
|
|
+
|
|
|
+ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
+ [dateFormatter setDefaultDate:[NSDate date]];
|
|
|
+ [dateFormatter setDateFormat:@"yyyy-MM-dd"];
|
|
|
+
|
|
|
+ DatePickerViewController* dpvc =[ [UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
|
|
|
+
|
|
|
+ __weak UITextField *weakField = field;
|
|
|
+ __weak typeof(self) weakSelf = self;
|
|
|
+
|
|
|
+ dpvc.pickerMode = UIDatePickerModeDate;
|
|
|
+ dpvc.date = [dateFormatter dateFromString:[dateFormatter stringFromDate:dateFormatter.defaultDate]];
|
|
|
+ dpvc.formatter = dateFormatter;
|
|
|
+ dpvc.blk_Set = ^(NSString *date) {
|
|
|
+ NSString *min = @"";
|
|
|
+ NSString *max = @"";
|
|
|
+ // 判断min是否大于max
|
|
|
+ if (weakField == weakSelf.minField) {
|
|
|
+ min = date;
|
|
|
+ max = [weakSelf.maxField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
|
|
|
+ if (max != nil && max.length > 1) {
|
|
|
+
|
|
|
+ NSTimeInterval min_interval = [dateFormatter dateFromString:min].timeIntervalSince1970;
|
|
|
+ NSTimeInterval max_interval = [dateFormatter dateFromString:max].timeIntervalSince1970;
|
|
|
+ if (min_interval > max_interval) {
|
|
|
+ [weakSelf showAlert];
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (weakField == weakSelf.maxField) {
|
|
|
+ min = [weakSelf.minField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
|
|
|
+ max = date;
|
|
|
+ if (min != nil && min.length > 1) {
|
|
|
+ NSTimeInterval min_interval = [dateFormatter dateFromString:min].timeIntervalSince1970;
|
|
|
+ NSTimeInterval max_interval = [dateFormatter dateFromString:max].timeIntervalSince1970;
|
|
|
+ if (min_interval > max_interval) {
|
|
|
+ [weakSelf showAlert];
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // max不小于min
|
|
|
+ [weakField setText:date];
|
|
|
+ if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(commonEditorRangeCell:didChangeMinValue:maxValue:atIndexPath:)]) {
|
|
|
+ [weakSelf.delegate commonEditorRangeCell:weakSelf didChangeMinValue:min maxValue:max atIndexPath:weakSelf.indexPath];
|
|
|
+ }
|
|
|
+ };
|
|
|
+ if (field == self.minField) {
|
|
|
+ dpvc.title = @"Select MinDate";
|
|
|
+ } else if (field == self.maxField) {
|
|
|
+ dpvc.title = @"Select MaxDate";
|
|
|
+ }
|
|
|
+
|
|
|
+ [[self viewController].navigationController pushViewController:dpvc animated:true];
|
|
|
+}
|
|
|
+
|
|
|
+@end
|