我有这段代码,BGBaseSnippetCode.h
:
static NSMutableDictionary * defaultHeightDictionary= nil;
static NSMutableDictionary * defaultBoundsDictionary =nil;
+(void)initialize
{
BGBaseTableViewCell * typical = [[self alloc]init];
if (defaultHeightDictionary==nil) {
defaultHeightDictionary = [NSMutableDictionary dictionary];
defaultBoundsDictionary = [NSMutableDictionary dictionary];
}
[defaultHeightDictionary setValue:@(typical.bounds.size.height) forKey:NSStringFromClass([self class])];
CGRect bounds = typical.bounds;
NSValue * boundsValue = [NSValue valueWithCGRect:bounds];
[defaultHeightDictionary setValue:boundsValue forKey:NSStringFromClass([self class])];
}
+(CGFloat) defaultHeight
{
NSNumber * result = [defaultHeightDictionary valueForKey:NSStringFromClass([self class])];
return result.floatValue;
}
+(CGRect) defaultBounds
{
NSValue * result = [defaultBoundsDictionary valueForKey:NSStringFromClass([self class])];
return [result CGRectValue];
}
我想在BGBaseOfAllUIControl.m
和中都插入这个BGBaseTableViewCell.m
。所以我只是尴尬地这样做了:
@interface BGBaseOfAllUIControl ()
@property (strong, nonatomic) IBOutlet UIView *view;
@end
@implementation BGBaseOfAllUIControl
#import "BGBaseSnippetCode.h"
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self BaseInitialize];
}
@interface BGBaseTableViewCell ()
@property (strong, nonatomic) IBOutlet UITableViewCell *view;
@end
@implementation BGBaseTableViewCell
//static BOOL isDefaultHeightSet = NO;
#import "BGBaseSnippetCode.h"
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self BaseInitialize];
}
return self;
基本上两者都BGBaseOfAllUIControl
共享BGBaseTableViewCell
相同的协议,我希望完全相同的代码为双方实现协议。BGBaseTableViewCell
是 的子类并且UITableViewCell
是BGBaseOfAllUIControl
的子类UIControl
。
所以我使用 .h 文件来包含一些实现。代码工作正常。只是尴尬。有什么更好的方法来做到这一点,或者我这样做对吗?