标签:
UITableView中cell里的UITextField不被弹出键盘挡住
效果如下:
源码:
EditCell.h 与 EditCell.m
// // EditCell.h // Cell // // Created by YouXianMing on 14/12/18. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @interface EditCell : UITableViewCell @property (nonatomic, strong) UITextField *field; @end
// // EditCell.m // Cell // // Created by YouXianMing on 14/12/18. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "EditCell.h" @implementation EditCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)]; line.backgroundColor = [UIColor colorWithRed:0.886 green:0.918 blue:0.933 alpha:1]; [self addSubview:line]; _field = [[UITextField alloc] initWithFrame:CGRectMake(20, 2, 280, 42)]; _field.textColor = [UIColor grayColor]; _field.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18]; [self addSubview:_field]; } return self; } @end
ViewController.m
// // ViewController.m // Cell // // Created by YouXianMing on 14/12/18. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ViewController.h" #import "EditCell.h" static NSInteger number = 8; @interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, weak) UITextField *tmpTextField; // 获取当前编辑的TextField @property (nonatomic, strong) NSMutableArray *strsArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 数据源(初始化) _strsArray = [NSMutableArray array]; for (int i = 0; i < number; i++) { [_strsArray addObject:@""]; } // 初始化tableView _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.backgroundColor = [UIColor colorWithRed:0.949 green:0.957 blue:0.961 alpha:1]; [self.view addSubview:_tableView]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [_tableView registerClass:[EditCell class] forCellReuseIdentifier:@"YouXianMing"]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return number; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { EditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YouXianMing"]; cell.field.delegate = self; cell.field.text = _strsArray[indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } #pragma mark - UITextField代理 - (void)textFieldDidBeginEditing:(UITextField *)textField { // 获取到临时的textField并存储起来 self.tmpTextField = textField; // 获取到父类cell EditCell *cell = (EditCell *)[self.tmpTextField superview]; // 获取到indexPath NSIndexPath *path = [self.tableView indexPathForCell:cell]; // 执行动画(移动到输入的位置) [self.tableView setContentOffset:CGPointMake(0, (path.row)*44) animated:YES]; } - (void)textFieldDidEndEditing:(UITextField *)textField { // 获取到临时的textField并存储起来 self.tmpTextField = textField; // 获取到父类cell EditCell *cell = (EditCell *)[self.tmpTextField superview]; // 获取到indexPath NSIndexPath *path = [self.tableView indexPathForCell:cell]; // 存储到数据源中 [_strsArray replaceObjectAtIndex:path.row withObject:(textField.text == nil ? @"" : textField.text)]; // 打印信息 NSLog(@"%@", _strsArray); } - (BOOL)textFieldShouldReturn:(UITextField *)sender { // 执行动画(恢复到原始位置) [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES]; // 交出第一响应者 [sender resignFirstResponder]; return YES; } @end
以下是需要注意的地方:
通过父视图获取到了cell,然后根据cell获取indexPath值,然后可以做事情了
核心代码是根据第几个cell来执行位移的动画,这个值是可以调整的。
UITableView中cell里的UITextField不被弹出键盘挡住
标签:
原文地址:http://www.cnblogs.com/YouXianMing/p/4174769.html