我的建议是单身。例如:
@interface ColorThemeSingleton : NSObject
@property (strong, atomic) UIColor *tableViewBackgroundColor;
+(ColorThemeSingleton *)sharedInstance;
@end
和.m
:
#import "ColorThemeSingleton.h"
@implementation ColorThemeSingleton
@synthesize tableViewBackgroundColor = _tableViewBackgroundColor;
+(ColorThemeSingleton *)sharedInstance{
static ColorThemeSingleton *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[ColorThemeSingleton alloc] init];
});
return shared;
}
-(id)init{
if ((self = [super init])){
_tableViewBackgroundColor = [UIColor whiteColor]; // Default color
}
return self;
}
@end
然后刚过if(cell==nil){}
在tableView:cellForRowAtIndexPath:
你可以这样:
cell.backgroundColor = [ColorThemeSingleton sharedInstance].tableViewBackgroundColor;
而当你从网络加载你的颜色,设置该属性的值为获取的颜色。下一次单元格运行tableView:cellForRowAtIndexPath:
时,颜色将成为新颜色。
因此,基本上只需将#import""
和cell.backgroundColor =
添加到tableView
的dataSource
即可。对我来说,这比改变每个控制器上的UITableViewCell
的课程要好得多。