I have added a copy property to UITableViewCell using category as shown below
NSString const *key = @"CellSelectedBlockKey";
@interface UITableViewCell (CellSelectionBlock)
@property (nonatomic, copy) CellSelectedBlock cellSelectedBlock;
@end
@implementation UITableViewCell (CellSelectionBlock)
- (void)setCellSelectedBlock:(void(^)(UITableView *table, NSIndexPath *indexpath))cellSelectedBlock
{
objc_setAssociatedObject(self, &key, cellSelectedBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CellSelectedBlock)cellSelectedBlock
{
return objc_getAssociatedObject(self, &key);
}
@end
How would I clear memory (release), this property as I cannot overite dealloc method for this.
I'll appreciate any help on this. Any suggestion on best way to add property to an existing class without subclassing.
An associated object set with a strong-reference associated object policy (OBJC_ASSOCIATION_RETAIN
, OBJC_ASSOCIATION_RETAIN_NONATOMIC
, OBJC_ASSOCIATION_COPY
, or OBJC_ASSOCIATION_COPY_NONATOMIC
) are automatically released when a new value is set (or nil
is set to clear) for the same key, or when the parent object is deallocated, just like with retain
/copy
properties, or with a strong reference in ARC.