2011-11-29 76 views
22

如果在分組的UITableViewCell上設置backgroundColor屬性,則背景顏色會成功更改。大。iOS:使用UIAppearance定義自定義UITableViewCell顏色

但是我想用UIAppearance改變我所有的UITableViewCells的背景顏色,所以我可以在一個地方做到這一點,並影響到處都有變化。這裏是我的代碼:

[[UITableViewCell appearance] setBackgroundColor:[UIColor colorWithRed:30.0/255.0 green:30.0/255.0 blue:30.0/255.0 alpha:1.0]]; 

的UITableViewCell實現UIAppearance和UIAppearanceContainer,所以我還以爲這會工作。但事實並非如此。我也試過使用-[UITableViewCell appearanceWhenContainedIn:(Class)],這也不起作用。

任何想法?

回答

41

更新(2013/7/8) - 這已在更新版本的iOS中修復。但是,如果您的目標是iOS 6或更低版本,則值得了解。

你可以責怪蘋果爲這一個,它實際上很不重要。 技術上,backgroundColor通過外觀代理不可定製

從蘋果公司的文檔:

爲了支持外觀定製,類必須符合UIAppearanceContainer協議和相關的訪問方法必須註明UI_APPEARANCE_SELECTOR。所以

@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR; 

,因爲它打上我們知道它的工作原理與UIAppearanceUI_APPEARANCE_SELECTOR標籤:

如果我們進入一類像UIBarButtonItem,並期待在tintColor屬性我們可以看到這一點。

這裏就是蘋果的特別意思是:在UIViewbackgroundColor沒有外觀選擇標籤,但UIAppearance仍然有效。根據蘋果提供的所有文件不應該,但它確實!

這給人以誤導的印象,它將適用於UIView的所有子類別,包括UITableView。這已經到來之前,in this previous SO answer

因此,底線是,backgroundColor不應UIAppearance在所有的工作,但由於某種原因它在UIView。不保證在UIView子類上工作,並且它根本不能在UITableView上工作。對不起,我無法給你一個更積極的答案!

+0

感謝您的解釋。因爲這個原因,我可能會花幾個小時在牆上點頭。 – matsr

+0

是的,很好的答案。謝謝! –

+0

如果這真的讓你感到困擾(它讓我困擾!),那麼肯定會向蘋果公司提交一份錯誤報告。我不認爲這本身就是一個「錯誤」,但這絕對是一個缺陷。我想我甚至可能會看到蘋果的某個人在代碼演示中使用'setBackgroundColor'作爲外觀代理。 – lxt

17

您可以創建自己的符合UIAppearance的UITableViewCell的子類,並用UI_APPEARANCE_SELECTOR標記自定義設置器。然後在自定義設置器的超類上設置單元格backgroundColor。

在你的appDelegate

[[CustomCell appearance] setBackgroundCellColor:[UIColor redColor]]; 

在你的UITableView子類

@interface CustomCell : UITableViewCell <UIAppearance> 

@property (nonatomic, weak) UIColor *backgroundCellColor UI_APPEARANCE_SELECTOR; 

@implementation CustomCell 

@synthesize backgroundCellColor; 

-(void)setBackgroundCellColor:(UIColor *)backgroundColor 
{ 
    [super setBackgroundColor:backgroundColor]; 
} 

我在這個例子中使用ARC。

+0

即使沒有子類化,也可以使用類別和自定義屬性來代理外觀調用。如果您想爲不能使用子類的部分進行主題化(例如,對於'UIPrintInteractionController'),它會很有用。 – Tom

0

我用這個分類。下面是示例代碼 在您的.h文件中寫

*@interface UITableViewCell (MyCustomCell) 
@property (nonatomic, weak) UIColor *backgroundCellColor UI_APPEARANCE_SELECTOR; 
@end* 

在您.m文件寫

*@dynamic backgroundCellColor; 
-(void)setBackgroundCellColor:(UIColor *)backgroundColor 
{ 
    [super setBackgroundColor:backgroundColor]; 
}* 

它的工作很適合我。 :) 謝謝奈特!

3

沒有子類在子類上執行此操作可能不是最佳實踐,特別是如果要打擊所有tableView背景。這是很多子類。很多潛在的錯誤。真是一團糟。最好的辦法是使用一個類別。你將不得不爲tableViewCell和tableView設置一個。我將只展示一個單元格。您必須執行的tableView屬性是backgroundColor屬性。 NB。我正在將我的方法預先添加爲「sat」。

// .H

#import <UIKit/UIKit.h> 

@interface UITableViewCell (Appearance)<UIAppearance> 
@property (strong, nonatomic) UIColor *satBackgroundColor UI_APPEARANCE_SELECTOR; 
@end 

// .M

#import "UITableViewCell+Appearance.h" 

@implementation UITableViewCell (Appearance) 

- (UIColor *)satBackgroundColor 
{ 
    return self.backgroundColor; 
} 

- (void)setSatBackgroundColor:(UIColor *)satBackgroundColor 
{ 
    self.backgroundColor = satBackgroundColor; 
} 


@end 

現在在你的appDelegate或您將導入的類別,只需要調用它,就好像它有一個外觀的一些經理級代理內置。

UITableViewCell *cell = [UITableViewCell appearance]; 
cell.satBackgroundColor = [UIColor orangeColor]; 

好的,所以現在只需做一個tableView的背景屬性。簡單。