2012-01-25 12 views
1

backgroundColorをiOSアプリに設定する最も簡単な方法は何ですか?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 

それからちょうどtableView:cellForRowAtIndexPath:if(cell==nil){}後、あなたが追加します。

cell.backgroundColor = [ColorThemeSingleton sharedInstance].tableViewBackgroundColor; 

そして、あなたがウェブ、セットからあなたの色をロードします取得されたカラーに対するプロパティの値次にセルがtableView:cellForRowAtIndexPath:を通過するとき、その色は新しい色になります。

基本的にはtableViewdataSource#import""cell.backgroundColor =を追加してください。私には、すべてのコントローラーでUITableViewCellのクラスを変更するよりもはるかに優れています。

関連する問題