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

本人视频教程系类 iOS中CALayer的使用
效果如下:

源码:
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值,然后可以做事情了

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

UITableView中cell里的UITextField不被弹出键盘挡住的更多相关文章
- UITableView中cell点击的绚丽动画效果
UITableView中cell点击的绚丽动画效果 本人视频教程系类 iOS中CALayer的使用 效果图: 源码: YouXianMingCell.h 与 YouXianMingCell.m / ...
- 如何获取UITableView中cell的frame值
如何获取UITableView中cell的frame值 这个可以用来处理UITableView弹出键盘的问题 本人视频教程系类 iOS中CALayer的使用 效果: 源码: // // ViewC ...
- 用适配器模式处理复杂的UITableView中cell的业务逻辑
用适配器模式处理复杂的UITableView中cell的业务逻辑 适配器是用来隔离数据源对cell布局影响而使用的,cell只接受适配器的数据,而不会与外部数据源进行交互. 源码: ModelCell ...
- 关于UITextfield弹出键盘解决方案
解决的问题:当你点击一个UITextfield时,不想让其弹出键盘,如果你觉得不就是取消其第一响应者嘛,resignRespond一下不就行了嘛,确实,如果你只是在其编辑完成后让其键盘消失,那这个就够 ...
- 关于Android中EditText自动获取焦点并弹出键盘的相关设置
在android开发中,关于EditText自动获取焦点弹出键盘,我们可能又是会有让键盘自动弹出的需求,有时可能又会有不想让键盘自动弹出的需求,下面是我所总结的两种方法: 需求:EditText自动获 ...
- 解决UITableView中Cell重用机制导致内容出错的方法总结
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
- ios UITableView中Cell重用机制导致内容重复解决方法
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
- iOS学习之UITableView中Cell的操作
接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
随机推荐
- maven tomcat插件上传项目到tomcat服务器报错SEVERE: One or more listeners failed to start.
以前觉了maven依赖设置很简单,就是将手动导入jar包转化为自动下载导入 但发现的一个问题, 在使用maven插件tomcat打包上传工具时 tomcat-maven-plugin <buil ...
- android学习-Adapter适配器进阶
参考资源 Android 快速开发系列 打造万能的ListView GridView 适配器 实现代码复用,争取打机**的时间. android4.4源码 target=android-19 一般自定 ...
- mongodb-分组分页
1, 添加测试数据 @Test public void save() { News n = null; ; i < ; i++) { n = new News(); n.setTitle(&qu ...
- springboot-23-aspectj日志记录及threadlocal内存泄漏
对于请求参数的处理和响应, 如果在代码中体现日志会显得很繁琐, 普遍的解决方案是使用spring的切面方案去解决. 这儿使用的是springboot的切面: http://www.cnblogs.co ...
- 9-lvs-lvs集群-及keepalived健康检查
注意: 配置前需要将上一篇的配置都清除掉 ifconfig eth1: down service ipvsadm restart nginx作为请求分发服务器时, 有健康检查机制, 挂了的服务器不会在 ...
- printf()函数中\t,水平制表符,空格的个数
在控制台输出数据的时候,也就是用printf()的时候,我们经常用\t来控制对齐,以使输出的结果更加整齐美观. 然而有时候我们发现及时使用了\t 也会出现数据对不齐的情况,这就跟\t究竟对应几个空格有 ...
- 前端富文本编辑器 vue-html5-editor
1..项目创建与初始化 在安装好脚手架的依赖后,要执行 npm install vue-html5-editor -S 来安装这个富文本插件,由于这个富文本插件的图标是依赖font-awesome.c ...
- VUE脚手架,babel转码 常用命令
vue-cli脚手架,单页面应用初始化时 npm -v 查看npm版本号 npm install vue-cli -g 全局安装vue-cli vue -V查看vue版本号,说明vue-cli已经安 ...
- BATJ面试必会之Java IO 篇
一.概览 二.磁盘操作 三.字节操作 实现文件复制 装饰者模式 四.字符操作 编码与解码 String 的编码方式 Reader 与 Writer 实现逐行输出文本文件的内容 五.对象操作 序列化 S ...
- C#语法之匿名函数和Lambda表达式
上一篇博客主要是对委托和事件做了一小结,这篇是在上一篇博客的基础上对匿名函数和Lambda表达式小结.还是接着上一篇说起,在上一篇中也说了委托是一种数据结构,主要是解决让函数作为参数的问题.在使用委托 ...