一.创建分类并添加属性:
分类中声明的属性并未自动生成get和set方法,这里通过runtime机制动态实现get和set方法。
1.创建.h文件:
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger,ReloadImageWay){
ReloadImageWayOne,
ReloadImageWayTwo,
};
@interface UITableViewCell (reloadImage)
@property (assign, nonatomic) ReloadImageWay cellReloadWay;
-(void)loadImage;
-(void)loadImageWayOne;
@end
2.创建.m文件:
#import "UITableViewCell+reloadImage.h"
#import <objc/runtime.h>
static char *Key = "ReloadWay";
@implementation UITableViewCell (reloadImage)
-(ReloadImageWay)cellReloadWay{
ReloadImageWay way = [(NSString *)objc_getAssociatedObject(self, Key) intValue];
return way;
}
-(void)setCellReloadWay:(ReloadImageWay)reloadImageWay{
[self willChangeValueForKey:@"cellReloadWay"];
objc_setAssociatedObject(self, Key, [NSString stringWithFormat:@"%d",reloadImageWay], OBJC_ASSOCIATION_COPY);
[self didChangeValueForKey:@"cellReloadWay"];
}
@end
二.调用分类中添加的属性
在用到属性的类里添加分类头文件,然后像正常添加的属性一样使用就ok了。