在 cell 中获取 textFlied内容的使用
当您读到这里时,建议先下载demo,不懂再参考博客。在iOS项目开发中,容易遇到各种个人信息填写。比如微信中设置个人信息,等。这种方式是进行控制器跳转,代理或者block传值,这种比较容易,符合常规的cell的应用场景。请继续往下看,后面更精彩!!!


#import <UIKit/UIKit.h>
@interface UITextField (IndexPath)
@property (nonatomic,strong)NSIndexPath *indexPath;
@end
#import "UITextField+IndexPath.h"
#import <objc/runtime.h> @implementation UITextField (IndexPath)
static char indexPathKey;
- (NSIndexPath *)indexPath{// get方法
return objc_getAssociatedObject(self, &indexPathKey);
} - (void)setIndexPath:(NSIndexPath *)indexPath{// set方法
// OBJC_ASSOCIATION_RETAIN_NONATOMIC 这个参数主要看单词的第三个,OC对象是retain或者copy,跟属性一个道理,
// OBJC_ASSOCIATION_ASSIGN 这是基本数据;类型需要的
objc_setAssociatedObject(self, &indexPathKey, indexPath,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
. .1HTextViewCell,.h中;自定义cell的写法。。 备注都给打好了??? ⚠️注意::加*号的注释非常重要
#import <UIKit/UIKit.h>
@interface HTextViewCell :UITableViewCell
/// 这里啰嗦两句:1.很多时候自定义cell在设置数据的时候都是模型复制,即.h中暴露一个属性,重写set方法,进行数据传值,
// 2.就是文中这种方式,数据传递,提供接口。。两者的优劣自己体会。
/// 提供接口,设置cell的数据
/// ****indexPath的作用,是每次把cell的indexPath索引穿进来,绑定到textfield中
- (void)setTitleString:(NSString *)string andDataString:(NSString *)dataString andIndexPath:(NSIndexPath *)indexPath;
@end
3.2. HTextViewCell .m中的文件
#import "HTextViewCell.h"
#import "UITextField+IndexPath.h" @interface HTextViewCell ()
@property (strong,nonatomic)UITextField *textField;
@property (strong,nonatomic)UILabel *titleLabel;
@end @implementation HTextViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {
[self.contentViewaddSubview:self.textField];
[self.contentViewaddSubview:self.titleLabel];
}
returnself;
} - (void)setTitleString:(NSString *)string andDataString:(NSString *)dataString andIndexPath:(NSIndexPath *)indexPath{
self.textField.indexPath = indexPath;// 这里展示每一行的数据的时候,indexPath和textile绑定一起了,这里又解决了疑问
self.textField.text = dataString;
self.titleLabel.text = string;
} - (UITextField *)textField{
if (!_textField) {
_textField = [[UITextFieldalloc]initWithFrame:CGRectMake(,,,)];
_textField.backgroundColor = [UIColorredColor];
}
return_textField;
} - (UILabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [[UILabelalloc]initWithFrame:CGRectMake(,,,)];
}
return_titleLabel;
}
@end
. 控制器中的代码,,ViewController.h的控制器。
#import "ViewController.h"
#import "HTextViewCell.h"
#import "UITextField+IndexPath.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong)UITableView *tableView; @property (nonatomic ,strong)NSArray *titleArray;
@property (nonatomic ,strong)NSMutableArray *arrayDataSouce; @end @implementation ViewController
- (void)dealloc{
[[NSNotificationCenterdefaultCenter]removeObserver:self];
} - (void)viewDidLoad {
[superviewDidLoad];
[self.viewaddSubview:self.tableView];
/// ********** 注册通知,监听键盘的输入情况
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textFieldDidChanged:)name:UITextFieldTextDidChangeNotificationobject:nil]; }
/// 保存数据的方法;
- (void)textFieldDidChanged:(NSNotification *)noti{
///*******这几句代码好好看看哦
UITextField *textField=noti.object;
NSIndexPath *indexPath = textField.indexPath;
[self.arrayDataSoucereplaceObjectAtIndex:indexPath.rowwithObject:textField.text];
}
#pragma marks - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
returnself.arrayDataSouce.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *Id =@"HTextViewCell";
HTextViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:Id];
if (!cell) {
cell = [[HTextViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:Id];
}
[cell setTitleString:self.titleArray[indexPath.row]andDataString:self.arrayDataSouce[indexPath.row]andIndexPath:indexPath];
return cell;
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.viewendEditing:YES];
}
#pragma marks- lazy
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableViewalloc]initWithFrame:CGRectMake(,,self.view.frame.size.width,self.view.frame.size.height)];
_tableView.delegate =self;
_tableView.dataSource =self;
}
return_tableView;
} - (NSMutableArray *)arrayDataSouce{
if (!_arrayDataSouce) {
_arrayDataSouce = [NSMutableArrayarray];
[_arrayDataSouceaddObject:@""];
[_arrayDataSouceaddObject:@""];
[_arrayDataSouceaddObject:@""]; }
return_arrayDataSouce;
}
- (NSArray *)titleArray{
if (!_titleArray) {
_titleArray =@[@"姓名:",@"年龄:",@"工作地点:"];
}
return_titleArray;
}
@end
五:到这里也句接近尾声了,总结一下就是为UITextField增加一个属性,用来和控制器中方法中的indexPath对应
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
后者就是监听键盘的方法,在获取通知内容,用来保存数据,最后附上一张运行效果图

原文地址http://blog.csdn.net/horisea/article/details/51872619
在 cell 中获取 textFlied内容的使用的更多相关文章
- iOS中获取cell中webview的内容尺寸
最近项目中遇到在cell中获取webView的内容的尺寸的需求 实现的思路其实很简单 就是通过执行js 获取尺寸即可 为了后面用着方便我直接封装了一个HTML的cell 起名就叫 STHTMLBase ...
- 从html字符串中获取div内容---jquery
思考的问题: 怎么在一个网页的div中嵌套另外的网页(不使用inclue,iframe和frame,不使用他们的原因,include只能嵌套静态网页,iframe对网络爬虫影响,frame嵌套网页无法 ...
- PHP中获取某个网页或文件内容的方法
1. 通过file_get_contents()函数$contents = file_get_contents('http://demo.com/index.php');echo $contents; ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- .net获取select控件中的文本内容
.net获取select控件中的文本内容 2009-11-28 21:19小V古 | 分类:C#/.NET | 浏览1374次 <select id="SecType" st ...
- UITableview 中获取非选中的cell
实现效果如图: 在cell中有一个button,选中cell改变button的选择状态 yes,选中另外一个cell,别的cell中的button选择状态变成false. //获取当前可显示的cell ...
- HtmlParser应用,使用Filter从爬取到的网页中获取需要的内容
htmlparser是一个纯的java写的html解析的库,它不依赖于其它的java库文件,主要用于改造或提取html.它能超高速解析html,而且不会出错.现在htmlparser最新版本为2.0. ...
- JS实现冒泡排序,插入排序和快速排序(从input中获取内容)
以前参加面试的时候,被问到过让用JS实现一个快速排序,当时太年轻,并没有回答上来. 于是,这里便把三种排序都用JS来做了一下.结合html,从input文本框中获取输入进行排序. 关于这几种算法的原理 ...
- 如何获取一个AlertDialog中的EditText中输入的内容
怎么获取一个AlertDialog中的EditText中输入的内容? new AlertDialog.Builder(this) .setTitle("请输入") .set ...
随机推荐
- ACM学习历程—NPU1045 2015年陕西省程序设计竞赛网络预赛(热身赛)C题 Graph Theory(递推 && 组合数学 && 大数)
Description In graph theory, a matching or independent edge set in a graph G = (V , E) is a set of e ...
- 如何解决outlook2013邮件规则for other machine的失效问题
如何解决outlook2013邮件规则for other machine的失效问题 问题描述:因为重装系统,outlook2013进去后->Rules and Alerts->发现所有原来 ...
- 京东ie6中轮播模块小图出现在大图上
请大家给个评论,给个支持!呵呵 本人最新一套模版小清新童装母婴日韩风全屏轮播(上线风暴),在审核时审核失败,报的是“ie6中全屏海报轮播是小图出现在大图中间的兼容性错误” 而本人本机出现的是小图基本上 ...
- Java网络与多线程系列之1:实现一个简单的对象池
前言 为什么要从对象池开始呢,先从一个网络IO操作的demo说起 比如下面这段代码,显而易见已经在代码中使用了一个固定大小的线程池,所以现在的重点在实现Runnble接口的匿名对象上,这个对象每次创建 ...
- POJ3580:SuperMemo
浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...
- hdu3037Saving Beans——卢卡斯定理
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3037 卢卡斯定理模板——大组合数的取模 代码如下: #include<iostream> #i ...
- poj1179多边形——区间DP
题目:http://poj.org/problem?id=1179 区间DP,值得注意的是有负值,而且有乘法,因此可能会影响最大值: 注意memset中写-1仅仅是-1,-2才是一个很小的负数: 最后 ...
- H5 开发
一.Html5手机站开发概述 Html5app开发就是HTML5开发语言制作的移动手机网站.移动站点顾名思义,就是指一切用移动终端访问的网络站点(通常指网站),像通常用的手机.PAD( ...
- AI-Info-Micron-Insight:通往完全自主之路
ylbtech-AI-Info-Micron-Insight:通往完全自主之路 1.返回顶部 1. 通往完全自主之路 自动驾驶汽车正在从未来梦想演变为当代现实,随着技术成熟,个人和公共交通将永远转变. ...
- CSharp读取配置文件的类(简单实现)
Reinventing the wheel 系列 CSharp 读取配置文件的类 简单实现(注意没有写) 本人对CS 不是很熟,库也不熟,所以到网上找个实现,并自己添加了点异常.如果只是读取信息,足够 ...