在 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 ...
随机推荐
- 【LeetCode】051. N-Queens
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 洛谷P3373线段树模板2
题目:https://www.luogu.org/problemnew/show/P3373 带乘的线段树,更新时把加的标记也乘一下,然后取值时先乘后加. 代码如下: #include<iost ...
- c# winform DataGridView 单元格的屏幕位置
首先取得DataGridView的坐标位置:int dgvX = dataGridView1.Location.X;int dgvY = dataGridView1.Location.Y;然后取得选中 ...
- kindeditor Springmvc 整和解决图片上传问题
1. 在编辑页面引入js <script type="text/javascript" charset="utf-8" src="${baseP ...
- jquery easyui datebox 的使用 .
jquery easyui datebox 的使用 . 分类: jquery-easyui2012-10-09 19:07 266人阅读 评论(0) 收藏 举报 目录(?)[+] 看了jquery e ...
- Jquery.ajax 详细解释 通过Http请求加载远程数据
首先请看一个Jquery.ajax的例子 $.ajax({ type: "GET", url: "/api/SearchApi/GetResults", dat ...
- 2018上海大都会 J-Beautiful Numbers(数位dp-模未知)
J-Beautiful Numbers 链接:https://www.nowcoder.com/acm/contest/163/J 来源:牛客网 时间限制:C/C++ 8秒,其他语言16秒 空间限制: ...
- lightoj1010【规律】
思路: 根据案例的规律其实已经猜的差不多了,answer=n*m/2; 有一条边是1的情况,也很好判断,answer=n*m; 就是有一条边是2的时候比较隐秘:是连续2*2一块可以填,然后2*2不填, ...
- Solr 6.7学习笔记(02)-- 配置文件 managed-schema (schema.xml) - Analyzer, tokenizer(4)
有些时候,我们需要自定义 fieldType.下面的例子就是自定义的 fieldType,<analyzer type="index"> 表示索引时怎么处理,<a ...
- VUE之环境安装
一.环境安装 软件安装: nodejs https://nodejs.org/en/ vscode https://code.visualstudio.com/docs/?dv=win python- ...