UITableView中cell里的UITextField不被弹出键盘挡住

时间:2023-03-08 18:16:07
UITableView中cell里的UITextField不被弹出键盘挡住

UITableView中cell里的UITextField不被弹出键盘挡住

UITableView中cell里的UITextField不被弹出键盘挡住

本人视频教程系类   iOS中CALayer的使用

效果如下:

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(, , , )];
line.backgroundColor = [UIColor colorWithRed:0.886 green:0.918 blue:0.933 alpha:];
[self addSubview:line]; _field = [[UITextField alloc] initWithFrame:CGRectMake(, , , )];
_field.textColor = [UIColor grayColor];
_field.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:];
[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 = ; @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 = ; 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:];
[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(, (path.row)*) 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(, ) animated:YES]; // 交出第一响应者
[sender resignFirstResponder]; return YES;
} @end

以下是需要注意的地方:

通过父视图获取到了cell,然后根据cell获取indexPath值,然后可以做事情了

UITableView中cell里的UITextField不被弹出键盘挡住

核心代码是根据第几个cell来执行位移的动画,这个值是可以调整的。

UITableView中cell里的UITextField不被弹出键盘挡住