我将 Apple 组件的任何改进都放在了一个超类中。BasicTableView.m 中应用了以下升级,它扩展了UITableView
:
1) 覆盖setBackgroundColor
以适用于 iOS 7 及更高版本,同时保持与 iOS 6 和 5 的向后兼容性。
2) 覆盖initWithCoder:
并将initWithFrame:style:
背景颜色初始化为默认透明背景
- (id) initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
[self setup];
}
return self;
}
- (id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
[self setup];
}
return self;
}
- (void) setup
{
//Set the BG color to clear color by default
[self setBackgroundColor:[UIColor clearColor]];
}
- (void) setBackgroundColor:(UIColor *)backgroundColor
{
if (self.style == UITableViewStyleGrouped) {
if (majorSystemVersion() < 7) {
UIView *tableBgView = [[UIView alloc] init];
tableBgView.backgroundColor = backgroundColor;
self.backgroundView = tableBgView;
} else {
[super setBackgroundColor:backgroundColor];
}
}
}
//majorSystemVersion() is defined elsewhere in a utility file
int majorSystemVersion(void) {
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
NSArray *systemVersionComps = [systemVersion componentsSeparatedByString:@"."];
NSString *majorVersion = [systemVersionComps objectAtIndex:0];
return [majorVersion intValue];
}