IOS Runtime属性关联实现表格编辑文本
要实现在表格里编辑文本, 表格让我想到了CollectionView,文本让我想起TextView, 做之前想了好久怎么样来获得编辑的是哪个TextView,要获取对应的IndexPath啊,想着之前Cell中的按钮用block来实现,在自定义的Cell中加一个属性存IndexPath,可想着就一个TextView要自定义写一个类这样也未免太麻烦了。正好突然想到之前听过的属性管理,自己就凑着这个机会用了下,赶脚还不错。
关联类型 | 等效的@property属性 |
OBJC_ASSOCIATION_ASSIGN | ASSIGN |
OBJC_ASSOCIATION_RETAIN_NONATOMIC | nonatomic,retain |
OBJC_ASSOCIATION_COPY_NONATOMIC | nonatomic,copy |
OBJC_ASSOCIATION_RETAIN | retain |
OBJC_ASSOCIATION_COPY | copy |
#import "ViewController.h" //属性关联属于动态运行时 #import <objc/runtime.h> //设置key const static NSString *key=@"indexPath"; @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITextViewDelegate> { CGFloat _imgHeight; UICollectionView *_mCollectionView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:[self createCollectionView]]; } //create CollectionView - (UICollectionView *)createCollectionView { // UICollectionViewDelegateFlowLayout UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; _imgHeight = (self.view.frame.size.width - )/; layout.itemSize = CGSizeMake((self.view.frame.size.width - )/, ); layout.minimumLineSpacing = ; layout.minimumInteritemSpacing = ; layout.sectionInset = UIEdgeInsetsMake(, , , ); _mCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout ]; _mCollectionView.delegate = self; _mCollectionView.dataSource = self; _mCollectionView.backgroundColor = [UIColor whiteColor]; [_mCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; return _mCollectionView; } //UICollectionViewDataSource,UICollectionViewDelegate - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { ; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { ) { ; } ; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%@",indexPath); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(, , _imgHeight,)]; textView.layer.borderWidth=; textView.layer.borderColor=[UIColor redColor].CGColor; [cell.contentView addSubview:textView]; //属性关联 设置 objc_setAssociatedObject(textView, key.UTF8String, indexPath , OBJC_ASSOCIATION_RETAIN); textView.delegate=self; return cell; } //UITextViewDelegate - (void)textViewDidEndEditing:(UITextView *)textView { //属性关联 获取 NSIndexPath *indexPath=objc_getAssociatedObject(textView, key.UTF8String); NSLog(@"%@",indexPath); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
IOS Runtime属性关联实现表格编辑文本的更多相关文章
- Xamarin iOS教程之显示和编辑文本
Xamarin iOS教程之显示和编辑文本 Xamarin iOS显示和编辑文本 在一个应用程序中,文字是非常重要的.它就是这些不会说话的设备的嘴巴.通过这些文字,可以很清楚的指定这些应用程序要表达的 ...
- 给iOS开发新手送点福利,简述文本属性Attributes的用法
给iOS开发新手送点福利,简述文本属性Attributes的用法 文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...
- iOS 9应用开发教程之编辑界面与编写代码
iOS 9应用开发教程之编辑界面与编写代码 编辑界面 在1.2.2小节中提到过编辑界面(Interface builder),编辑界面是用来设计用户界面的,单击打开Main.storyboard文件就 ...
- JAVAEE——BOS物流项目09:业务受理需求分析、创建表、实现自动分单、数据表格编辑功能使用方法和工作单快速录入
1 学习计划 1.业务受理需求分析 n 业务通知单 n 工单 n 工作单 2.创建业务受理环节的数据表 n 业务通知单 n 工单 n 工作单 3.实现业务受理自动分单 n 在CRM服务端扩展方法根据手 ...
- 包建强的培训课程(11):iOS Runtime实战
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- IOS runtime动态运行时二
在C#.Java中有编译时多态和运行时多态,在OC中,只有运行时的多态,这与它的运行机制有关.OC中,方法的调用是通过消息的传递来进行的.在IOS runtime动态运行时一http://www.cn ...
- iOS --runtime理解
iOS~runtime理解 Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门语言所必需理解的东西.最近在学习Runtime,有自己的一些心得,整理如下,一为 查阅方便二为 或许能给 ...
- SNF开发平台WinForm之一-开发-单表表格编辑管理页面-SNF快速开发平台3.3-Spring.Net.Framework
1.1运行效果: 1.2开发实现: 1.2.1 首先在数据库中创建需要开发的数据表,在代码生成器中进行配置连接数据库. 代码生成器的Config.xml文件配置如下节点: 1.2.2 ...
- ios runtime的相关知识
一.iOS runtime原理 对于runtime机制,在网上找到的资料大概就是怎么去用这些东西,以及查看runtime.h头文件中的实现,当然这确实是一种很好的学习方法,但是,其实我们还是不会知道r ...
随机推荐
- Angular之constructor和ngOnInit差异及适用场景
constructor会在类生成实例时调用,Angular无法控制constructor,constructor中应该只进行依赖注入而不是进行真正的业务操作 ngOnInit属于Angular生命周期 ...
- 浅谈 温故知新——HTML5!
古人有云:温故而知新.活到老,学到老,作为一枚前端的程序猿,不停的学习能够让我们对我们的技术有一个更加丰富的认识.这几天,项目已经完成,但我发现自己的知识体系存在一些短板,特别是在H5方面,所以我又回 ...
- WPF带小箭头的按钮
XAML代码: <ControlTemplate x:Key="btnTpl" TargetType="RadioButton"> <Stac ...
- 获取BinaryReader中读取的文件名
BinaryReader br; br = null; br = new BinaryReader(new FileStream("E:demo.txt", FileMode.Op ...
- 关于Mysql数据库进行多表查询时设计编程思想
SQL代码:
- jmeter ——JDBC Request中从数据库中读两个字段给接口取值
前置条件数据库: 给接口传:tid和shopid这俩字段 直接从JDBC Request开始: Variable name:这里写入数据库连接池的名字(和JDBC Connection Configu ...
- socketserver模块解析
socketserver模块是基于socket而来的模块,它是在socket的基础上进行了一层封装,并且实现并发等功能. 看看具体用法: x import sockets ...
- Java 自定义注解与注解解析实例
在学习Java之后会遇到很多的注解,有加载JavaBean的注解:@Component,@Service,@Controller:有获取配置文件中数值的注解@Value:有获取Http请求的数据的注解 ...
- Dota2APP--第一天
从今天开始,下定决心自己独立开发一个有关于Dota2的APP,因为非常喜欢这个游戏恰好自己又是做iOS移动开发的所以萌生了这个想法.希望可以坚持下去,有喜欢dota2的朋友也可以提点建议. 一.项目的 ...
- string类型介绍
一.前言 int,float,char,C++标准库提供的类型:string,vector. string:可变长字符串的处理:vector一种集合或者容器的概念. 二.string类型简介 C++标 ...