2012-01-25 23 views
1

在iOS应用程序中设置所有UITableViewCellbackgroundColor最简单的方法是什么?在iOS应用程序中为所有UITableViewCell设置背景颜色

考虑一个具有相对较大数量的UITableViewController子类的应用程序。背景颜色是通过Web服务指定的。所以在每个tableViewController中使用颜色可能不是最容易做的事情。

也许每个tableViewCell都可以继承UITableViewCell子类,其中的颜色是为所有派生类设置的?可能有一个更简单的方法。

回答

1

我的建议是单身。例如:

@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 =添加到tableViewdataSource即可。对我来说,这比改变每个控制器上的UITableViewCell的课程要好得多。