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不被弹出键盘挡住的更多相关文章

  1. UITableView中cell点击的绚丽动画效果

    UITableView中cell点击的绚丽动画效果 本人视频教程系类   iOS中CALayer的使用 效果图: 源码: YouXianMingCell.h 与 YouXianMingCell.m / ...

  2. 如何获取UITableView中cell的frame值

    如何获取UITableView中cell的frame值 这个可以用来处理UITableView弹出键盘的问题 本人视频教程系类   iOS中CALayer的使用 效果: 源码: // // ViewC ...

  3. 用适配器模式处理复杂的UITableView中cell的业务逻辑

    用适配器模式处理复杂的UITableView中cell的业务逻辑 适配器是用来隔离数据源对cell布局影响而使用的,cell只接受适配器的数据,而不会与外部数据源进行交互. 源码: ModelCell ...

  4. 关于UITextfield弹出键盘解决方案

    解决的问题:当你点击一个UITextfield时,不想让其弹出键盘,如果你觉得不就是取消其第一响应者嘛,resignRespond一下不就行了嘛,确实,如果你只是在其编辑完成后让其键盘消失,那这个就够 ...

  5. 关于Android中EditText自动获取焦点并弹出键盘的相关设置

    在android开发中,关于EditText自动获取焦点弹出键盘,我们可能又是会有让键盘自动弹出的需求,有时可能又会有不想让键盘自动弹出的需求,下面是我所总结的两种方法: 需求:EditText自动获 ...

  6. 解决UITableView中Cell重用机制导致内容出错的方法总结

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  7. ios UITableView中Cell重用机制导致内容重复解决方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  8. iOS学习之UITableView中Cell的操作

    接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...

  9. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

随机推荐

  1. Nginx安装图片模块出错,提示fatal error: curl/curl.h

    获得安装包,从网上直接下载下载地址:https://curl.haxx.se/download.html 然后解压安装后就可以了 # # cd curl- # ./configure # make & ...

  2. MySQL decimal unsigned 更新负数不报错却为0

    今天在验证接口的并发问题时,把之前通过 redis 解决的并发压力转移到 mysql 上(redis 在 set 保存数据和数据过期需要去向数据库获取时存在时延,会存在空挡造成大并发多插入数据的风险: ...

  3. 高级网络功能(Docker支持的网络定制配置)

    网络的高级知识,包括网络的启动和配置参数.DNS的使用配置.容器访问和端口映射的相关实现. 在一些具体场景中,Docker支持的网络定制配置,通过Linux命令来调整.补充.甚至替换Docker默认的 ...

  4. hadoop下安装mahout

    安装hadoop 完成 安装mahout 首先下载mahout压缩文件apache-mahout-distribution-0.12.2.tar.gz 放到/home/hadoop/software- ...

  5. C语言中两个相同类型的结构体变量之间是可以相互直接赋值的

    C语言中,在相同类型的变量间赋值时是直接内存复制的,即将他们的内存进行复制,而两个同类型的结构体变量属于同一种变量,所以赋值时是按照他们的内存分布来直接拷贝的.所以,在C语言中两个相同类型的结构体变量 ...

  6. @RestController和@Controller注解的区别

    @RestController是@ResponseBody和@Controller注解的结合,当你return一个页面时,使用@Controller注解返回的是相应页面,使用@RestControll ...

  7. JavaScript窗口打开与关闭及如何使用opener使子窗口对父窗口进行操作

    一.打开与关闭窗口 1.打开窗口:可以使用window对象中的Open()方法. newWindow = window.open(url,windowname,location); 参数说明: url ...

  8. 单击GridView进入编辑模式

    一直以来,Insus.NET在实现GridView编辑时,均是在每笔记录第一列或是最后一列放置编辑铵钮,点击编辑铵钮之后,进行编辑模式.本博文是使用另外方式,即是点击GridView记录行任一位置,进 ...

  9. C# byte 和 char 转化

    C#  byte 和 char 可以认为是等价的.但是在文本显示的时候有差异.   c# 使用的是unicode字符集,应该和为ascii相互转换 只能转换到字符的unicode编码,或者由unico ...

  10. C# 进程优先级和线程优先级的方法

    C# 设置进程优先级的方法 this.process1= Process.GetCurrentProcess(); process1.PriorityClass = ProcessPriorityCl ...