// // FeedbackViewController.m // watch // // Created by xTT on 2017/7/13. // Copyright © 2017年 xTT. All rights reserved. // #import "FeedbackViewController.h" @interface FeedbackViewController () @property (strong, nonatomic) NSMutableDictionary *parameters; @end @implementation FeedbackViewController @synthesize myDataSource = _myDataSource; - (NSMutableArray *)myDataSource{ if (!_myDataSource) { _myDataSource = [[NSMutableArray alloc] initWithArray:@[@[@"邮箱"],@[@"反馈内容"]]]; } return _myDataSource; } - (NSMutableDictionary *)parameters{ if (!_parameters) { _parameters = [NSMutableDictionary dictionary]; } return _parameters; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (BOOL) validateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; return [emailTest evaluateWithObject:email]; } - (IBAction)rightBarItemClick:(UIBarButtonItem *)sender{ if ([self.parameters[@"email"] length] == 0) { [UICommon MessageErrorText:@"请输入您的邮箱"]; }else if(![self validateEmail:self.parameters[@"email"]]){ [UICommon MessageErrorText:@"请输入正确的邮箱"]; }else if ([self.parameters[@"content"] length] == 0){ [UICommon MessageErrorText:@"请输入您的宝贵意见"]; }else if ([self.parameters[@"content"] length] > 500){ [UICommon MessageErrorText:@"反馈意见不能超过500字"]; }else{ [xMyHttp URL:@"getway/feedback" method:@"POST" parameters:self.parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { [self goBack:sender]; } } failure:^(NSURLSessionDataTask *task, NSError *error) {}]; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; if ([title hasPrefix:@"反馈内容"]){ return 160; } return 49; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; if ([title hasPrefix:@"邮箱"]) { TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell"]; if (!cell) { cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TextFieldCell"]; cell.accessoryType = UITableViewCellAccessoryNone; cell.textField.placeholder = @"请输入您的邮箱"; cell.textField.keyboardType = UIKeyboardTypeEmailAddress; } cell.textLabel.text = title; WEAKSELF cell.block = ^(NSString *text, TextFieldCell *blockCell){ [weakSelf.parameters setObject:text forKey:@"email"]; }; return cell; }else if([title isEqualToString:@"反馈内容"]){ baseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"baseCell"]; if (!cell) { cell = [[baseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"baseCell"]; cell.accessoryType = UITableViewCellAccessoryNone; UITextView *textView = [[UITextView alloc] init]; textView.delegate = self; textView.backgroundColor = [UIColor whiteColor]; [cell.contentView addSubview:textView]; [textView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(cell.contentView); }]; UILabel *label = [[UILabel alloc] init]; label.tag = 1000; label.textColor = [UIColor colorWithWhite:0.5 alpha:0.4]; label.font = [UIFont systemFontOfSize:16]; [textView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.equalTo(textView).offset(5); }]; label.text = @"请提出您的宝贵意见"; } return cell; } return nil; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ if (section == self.myDataSource.count - 1) { return 10 + 44 + 10; } return [super tableView:tableView heightForFooterInSection:section]; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; if (section == self.myDataSource.count - 1) { UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(14, 10, SWIDTH-28, 44)]; [btn setTitle:@"提交" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [btn setBackgroundColor:mainColor]; [btn addTarget:self action:@selector(rightBarItemClick:) forControlEvents:UIControlEventTouchUpInside]; btn.layer.cornerRadius = 3; btn.layer.masksToBounds = YES; [view addSubview:btn]; } return view; } - (void)textViewDidChange:(UITextView *)textView{ UILabel *lable = [textView viewWithTag:1000]; if (textView.text.length > 0) { lable.hidden = YES; }else{ lable.hidden = NO; } [self.parameters setObject:textView.text forKey:@"content"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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