要实现在表格里编辑文本, 表格让我想到了CollectionView,文本让我想起TextView, 做之前想了好久怎么样来获得编辑的是哪个TextView,要获取对应的IndexPath啊,想着之前Cell中的按钮用block来实现,在自定义的Cell中加一个属性存IndexPath,可想着就一个TextView要自定义写一个类这样也未免太麻烦了。正好突然想到之前听过的属性管理,自己就凑着这个机会用了下,赶脚还不错。
有时候类的实例可能是某种机制所创建,而开发者无法令这种机制创建出自己所写的子类实例。
可以给某对象关联许多其他对象。这些对象会通过“键”来区分。
存储对象值的时候,可以指明“存储策略”,用以维护相应的“内存管理语义”。
存储策略由名为objc_AssociationPolicy 的枚举定义。
如果关联对象成了属性,那么它就会具备对应的语义。
关联类型 | 等效的@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 |
以下方法用来管理关联对象:
① void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy);
此方法以给定的键和策略为某对象设置关联对象值。
② void objc_getAssociatedObject(id object, void *key);
此方法根据给定的键从某对象中获取相应的关联对象值。
③ void objc_removeAssociatedObjects(id object);
此方法移除指定对象的全部关联对象。
4. 设置关联对象用的键是个“不透明的指针”,即所指向的数据结构不局限于某种特定类型的指针。
再设置关联对象值时,若想令两个键匹配到同一个值,则二者必须时完全相同的指针才行。
跟NSDictionary不一样。NSDictionary认为“isEqual:”返回YES,则二者相同。
故设置关联对象值时,通常使用静态全局变量做键。
代码:
#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 - 50)/4;
layout.itemSize = CGSizeMake((self.view.frame.size.width - 50)/4, 30);
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 10;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
_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
{
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section==0) {
return 10;
}
return 20;
}
- (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(0, 0, _imgHeight,30)];
textView.layer.borderWidth=2;
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