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也是可以不自定义的(比如某一个简单的 ...
随机推荐
- pcap简单使用和简单解释
数据类型bpf_u_int32实际上就是u_int的一个别名,还有吧bpf_int32实际上就是int的别名.当然这个int是32位的,如果操作系统对int的定义不是4字节,bpf_int32就对应另 ...
- EF Core 实现多租户
目录 SAAS 和多租户 多租户数据隔离方案 使用 EF Core 简单实现多租户 单数据库实现 多数据库实现 源代码 参考 SAAS 和多租户 SaaS(软件及服务)区别于其他应用程序的主要特征就是 ...
- ABP实战--集成Ladp/AD认证
参照Hunter的ABP-Zero模块中用户管理部分. 由于我们公司的各系统基本都是AD帐号登录的,所以我们需扩展ABP的AuthenticationSource. 添加MyLdapAuthentic ...
- 通用数据库连接池-C3PO
C3PO是一个开放源代码的JDBC数据连接池实现项目,实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.开源项目在使用:Hibernate,Spring,MYSQL等. 下载: h ...
- nginx 学习笔记(5) nginx调试日志
为启动一个调试日志,nginx需要在构建时配置城支持调试模式. ./configure --with-debug ... 而且调试级别应该使用err_log指令来设置: err_log /path/t ...
- JAVA-7NIO之Socket/ServerSocket Channel
一.ServerSocketChannel Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样.Ser ...
- [转]How to Create an Add-in for Microsoft Outlook
本文转自:https://www.codeproject.com/Articles/1112815/How-to-Create-an-Add-in-for-Microsoft-Outlook This ...
- [转]VS2012正则查找
本文转自:http://blog.csdn.net/u013688451/article/details/52840325 工作中有时需要用到 正则查找,例如 想找 所有用到 某个数据库表的地方 st ...
- 关于Win8快速启动失效解决
注册表修改HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager 下BootExecute值为 autocheck空格 ...
- 3YAdmin-专注通用权限控制与表单的后台管理系统模板
3YAdmin基于React+Antd构建.GitHub搜索React+Antd+Admin出来的结果没有上百也有几十个,为什么还要写这个东西呢? 一个后台管理系统的核心我认为应该是权限控制,表单以及 ...