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也是可以不自定义的(比如某一个简单的 ...
随机推荐
- __name__ == "__main__"的作用是什么?
问题: __name__ == "__main__" 的作用是什么? # Threading example import time, thread def myfunction( ...
- solidity如何拼接字符串?
当你开始学习使用solidity开发以太坊智能合约之后,很快你会碰到一个问题: 一.在solidity中该如何拼接字符串? 可能你已经试过了,下面的代码试图把两个字符串使用相加的运算符连接起来,但是这 ...
- Java简单的RPC实现(一)
RPC使用java最基本的,传输层使用Socket,序列化使用Serializable,java 动态代理模式,但是未实现消息注册等相关信息 大道至简 server端 package com.rpc. ...
- ruby 数据sql操作
ActiveRecord ActiveRecord 是 Rails 的 ORM 元件,負責與資料庫溝通,讓我們可以用物件導向的語法操作資料庫.在”打造 CRUD 應用程式”一章中提到的對應概念如下: ...
- rails 过滤掉所有的html标签 strip_tags
strip_tags(html) Strips all HTML tags from the html, including comments. This usesthe html-scanner ...
- [PY3]——logging
logging模块的logger.handler.filter.formatter Logger记录器 提供日志接口,供应用代码使用.logger最长用的操作有两类:配置和发送日志消息.可以通过log ...
- [javaSE] 数组(排序-冒泡排序)
两层嵌套循环,外层控制循环次数,内层循环进行比较 for(int x=0;x<arr.length-1;x++){ for(int y=0;y<arr.length;y++){ if(ar ...
- 算法 - Catalan数 (卡特兰)
http://blog.csdn.net/linhuanmars/article/details/24761459 https://zh.wikipedia.org/wiki/%E5%8D%A1%E5 ...
- Exam E05-001 Information Storage and Management Version 3 Exam
Emc 考试 e05-001信息存储和管理版本3考试 [总问题:171] 哪种 emc 产品提供软件定义的存储基础架构的自动监视和报告? A. viprSrmB. 斯纳普内C. 阿瓦马尔D. 快速副总 ...
- PHP CURL库学习
基本请求步骤 : // . 初始化 $ch = curl_init(); // . 设置选项,包括URL curl_setopt($ch, CURLOPT_URL, "http://www. ...