2012-01-25 2 views
1

iOS 앱에서 모두의 backgroundColor을 설정하는 가장 쉬운 방법은 무엇입니까?iOS 앱의 모든 UITableViewCell에 대한 배경색 설정

UITableViewController 개의 서브 클래스가 비교적 많이있는 앱을 생각해보십시오. 배경색은 웹 서비스를 통해 지정됩니다. 따라서 각 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:까지 실행되면 색이 새 색이됩니다.

기본적으로 과 cell.backgroundColor =tableViewdataSource에 추가하면됩니다. 모든 컨트롤러에서 UITableViewCell의 클래스를 변경하는 것보다 훨씬 좋습니다.

관련 문제