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 ...
随机推荐
- C#语言各个版本特性(三)
三.查询集合 1.找出List<Product>列表中符合特定条件的所有元素 C#1.1 查询步骤:循环,if判断,打印 product类 using System.Collections ...
- django drf 权限permission
https://www.django-rest-framework.org/api-guide/permissions/#custom-permissions from django.shortcut ...
- 【题解】 UVa11210 Chinese Mahjong
pdf传送门 直接模拟+搜索,考虑一下选哪一个是将,然后搜出顺子和刻子,最后判断一下可不可行就好了! #include<stdio.h> #include<string.h> ...
- Android 中 LayoutParams 的用法
一个控件应当使用它的父控件的 LayoutParams 类型.因此,一个 TableVow 应该使用 TableLayout.Params . 所以,以一个 TableRow 为例: TableRow ...
- Delphi XE7中开发安卓程序一些有用的帮助资源
说明:要想看到下面资源,打开帮助后,在搜索里面输入URL最后一部分,如果最后带“.html”,则把它删除,随后搜索一下,就看到了. 例如:想看下面的FireMonkey类关系图,只在搜索里面输入&qu ...
- 【OCP认证12c题库】CUUG 071题库考试原题及答案(28)
28.choose the best answer Evaluate the following SQL statement: SQL> SELECT promo_id, promo_categ ...
- LOJ#6044. 「雅礼集训 2017 Day8」共(Prufer序列)
题面 传送门 题解 答案就是\(S(n-k,k)\times {n-1\choose k-1}\) 其中\(S(n,m)\)表示左边\(n\)个点,右边\(m\)个点的完全二分图的生成树个数,它的值为 ...
- DESTOON从CSRF到GETSHELL
本文作者:薄荷糖微微凉 Destoon B2B网站管理系统是一套完善的B2B(电子商务)行业门户解决方案.系统基于PHP+MySQL开发,采用B/S架构,模板与程序分离,源码开放.模型化的开发思路,可 ...
- [JS] 瀑布流加载
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- Security-OAuth2.0 密码模式之客户端实现
我的OAuth2.0 客户端项目目录 pom 的配置 <?xml version="1.0" encoding="UTF-8"?> <proj ...