#import "EHExaminationPlanVC.h"
@interface EHExaminationPlanVC ()<UIGestureRecognizerDelegate, UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *detailTextArray;
@property (nonatomic, strong) UIDatePicker *mDatePicker; // 日期选择控件
@property (nonatomic, strong) UIView *mDateView;
@property (nonatomic, strong) UIAlertView *alert;
@end
@implementation EHExaminationPlanVC
- (UIView *)mDateView
{
if (!_mDateView) {
_mDateView = [[UIView alloc] initWithFrame:CGRectMake(0, Main_Screen_Height - 240, Main_Screen_Width, 240)];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 40)];
topView.backgroundColor = [UIColor whiteColor];
[_mDateView addSubview:topView];
UIButton *cancelButton = [toolset createButtonWithText:@"取消"
withType:UIButtonTypeCustom
withFrame:CGRectMake(1, 0, 60, 40)
withFont:SYSTEMFONT(14)
withNormalColor:[UIColor blueColor]
withHightColor:[UIColor grayColor]
withBackgroundColor:nil
withDelegate:self
withSelector:@selector(buttonClick:)
withNormaleImage:nil
withLightImage:nil];
cancelButton.tag = 200;
[topView addSubview:cancelButton];
UIButton *OKButton = [toolset createButtonWithText:@"确认"
withType:UIButtonTypeCustom
withFrame:CGRectMake(Main_Screen_Width - 60, 0, 60, 40)
withFont:SYSTEMFONT(14)
withNormalColor:[UIColor blueColor]
withHightColor:[UIColor grayColor]
withBackgroundColor:nil
withDelegate:self
withSelector:@selector(buttonClick:)
withNormaleImage:nil
withLightImage:nil];
OKButton.tag = 201;
[topView addSubview:OKButton];
UIView *topSeparateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 1)];
topSeparateView.backgroundColor = kColorAlpha(216, 216, 216, 1);
[topView addSubview:topSeparateView];
UIView *bottomSeparateView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, Main_Screen_Width, 1)];
bottomSeparateView.backgroundColor = kColorAlpha(216, 216, 216, 1);
[topView addSubview:bottomSeparateView];
_mDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, Main_Screen_Width, 200)];
_mDatePicker.datePickerMode = UIDatePickerModeDate;
_mDatePicker.backgroundColor = [UIColor whiteColor];
_mDatePicker.hidden = NO;
[_mDateView addSubview:_mDatePicker];
UIApplication *app = [UIApplication sharedApplication];
[app.keyWindow addSubview:_mDateView];
}
return _mDateView;
}
- (NSArray *)detailTextArray
{
if (!_detailTextArray) {
_detailTextArray = [self detailTextArrayData];
}
return _detailTextArray;
}
- (NSArray *)detailTextArrayData
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *timeStr = [defaults stringForKey:TestTime];
if (!timeStr)
timeStr = @"点击设置";
NSString *testResultStr = [defaults stringForKey:TestExpectedResults];
if (!testResultStr)
testResultStr = @"点击设置";
// else
// {
// if ([testResultStr intValue] > 100)
// testResultStr = @"100";
// }
NSString *testCityStr = [defaults stringForKey:TestCity];
if (!testCityStr)
testCityStr = @"待定";
return [NSArray arrayWithObjects:timeStr, testResultStr, testCityStr, nil];
}
- (NSArray *)titleArray
{
if (!_titleArray) {
_titleArray = [NSArray arrayWithObjects:@"考试时间", @"期望成绩", @"考场城市", nil];
}
return _titleArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"考试计划";
// self.mTableView.backgroundColor = [UIColor whiteColor];
self.mTableView.tableFooterView = [[UIView alloc] init];
// 创建点击手势
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenView:)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
recognizer.delegate = self;
[self.mTableView addGestureRecognizer:recognizer];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.mDateView.hidden = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark ------ tableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
if (indexPath.row == 2)
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
else
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = self.titleArray[indexPath.row];
cell.detailTextLabel.text = self.detailTextArray[indexPath.row];
cell.detailTextLabel.textColor = [UIColor blueColor];
return cell;
}
#pragma mark ------ tableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
switch (indexPath.row) {
case 0:
{
self.mDateView.hidden = NO;
}
break;
case 1:
{
[self modifyTheDesiredResults];
self.mDateView.hidden = YES;
}
break;
case 2:
{
self.mDateView.hidden = YES;
}
break;
default:
break;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 30)];
NSLog(@"%@", NSStringFromCGRect(tableView.tableHeaderView.bounds));
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 30)];
titleLabel.text = @"托福考试计划";
titleLabel.textColor = [UIColor grayColor];
titleLabel.font = SYSTEMFONT(14);
[header addSubview:titleLabel];
return header;
}
#pragma mark 日期选择取消和确定
- (void)buttonClick:(UIButton *)button
{
switch (button.tag) {
case 200:
self.mDateView.hidden = YES;
break;
case 201:
{
NSDate *date = [self.mDatePicker date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy年MM月dd日"];
NSString *dateStr = [dateformatter stringFromDate:date];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:dateStr forKey:TestTime];
[defaults synchronize];
self.detailTextArray = [self detailTextArrayData];
[self.mTableView reloadData];
self.mDateView.hidden = YES;
}
break;
default:
break;
}
}
- (void)hiddenView:(UIGestureRecognizer *)recognizer
{
self.mDateView.hidden = YES;
}
#pragma maek 修改期望的成绩
- (void)modifyTheDesiredResults
{
self.alert = [[UIAlertView alloc] initWithTitle:@"" message:@"期望成绩" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"修改", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[self.alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
self.alert.delegate = self;
[self.alert show];
UITapGestureRecognizer *recognizerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTapBehind:)];
[recognizerTap setNumberOfTapsRequired:1];
recognizerTap.cancelsTouchesInView = NO;
[[UIApplication sharedApplication].keyWindow addGestureRecognizer:recognizerTap];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
break;
case 1:
{
//得到输入框
UITextField *textField = [alertView textFieldAtIndex:0];
NSLog(@"%@", textField.text);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:textField.text forKey:TestExpectedResults];
[defaults synchronize];
self.detailTextArray = [self detailTextArrayData];
[self.mTableView reloadData];
} break;
default:
break;
}
}
#pragma mark 点击外包区域隐藏alertView
- (void)handleTapBehind:(UITapGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateEnded){
CGPoint location = [sender locationInView:nil];
if (![self.alert pointInside:[self.alert convertPoint:location fromView:self.alert.window] withEvent:nil]){
[self.alert.window removeGestureRecognizer:sender];
[self.alert dismissWithClickedButtonIndex:0
animated:YES];
}
}
}
// 手势冲突
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
return YES;
}
@end