2011-06-14 91 views
3

我的子類的UITableViewCell到單元格的背景顏色設置爲一種顏色,我需要:UITableViewCell的背景色問題

.H

@interface DataViewCustomCell : UITableViewCell { 
    UIColor* cellColor; 
    UIColor* standardColor; 
} 
- (void) setCellColor: (UIColor*)color; 

@end 

.M

@implementation DataViewCustomCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void) setCellColor: (UIColor*)color 
{ 
    cellColor = color; 
} 

- (void) spreadBackgroundColor: (UIView*)that withColor: (UIColor*)bkColor 
{ 
    NSEnumerator *enumerator = [that.subviews objectEnumerator]; 
    id anObject; 

    while (anObject = [enumerator nextObject]) { 
     if([anObject isKindOfClass: [UIView class]]) 
     { 
      ((UIView*)anObject).backgroundColor = bkColor; 
      [self spreadBackgroundColor:anObject withColor:bkColor]; 
     } 
    } 
} 

- (void) layoutSubviews { 
    [super layoutSubviews]; // layouts the cell as UITableViewCellStyleValue2 would normally look like 

    if(!self.selected && NULL != cellColor) 
    { 
     [self spreadBackgroundColor:self withColor:cellColor]; 
    } 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

當我打電話setCellColor與我想要的顏色一切都很順利,但是當我還沒有找到方法來設置原始顏色時:當我使用UITableViewStylePlain樣式設置[UIColor clearColor]時,結果不太好看。

Wrong result

我怎樣才能達到良好的效果,且不欠缺細胞分離機線?

回答

8

最後,我自己解決了。我用下面的代碼- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,而不是子類:

UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; 
    bg.backgroundColor = [UIColor greenColor]; //The color you want 
    cell.backgroundView = bg; 
    cell.textLabel.backgroundColor = bg.backgroundColor; 
    [bg release]; 
0

爲什麼不在設置新的單元格顏色之前,捕獲UIColor變量中的舊單元格顏色。 嘗試 UIColor * originalColor = anObject.backgroundColor; 然後當您想要更改爲只更改單元格顏色爲originalColor時。

+0

它不工作 – edo42 2011-06-14 16:49:18

+0

它有什麼作用?根本改變顏色? – James 2011-06-14 16:57:22

+0

我有一些內存問題,但我修好了,它返回灰色 – edo42 2011-06-14 17:00:04

10

我有一個類似的問題,發現edo42的答案。然而,當時我的單元格背後的背景背景出現問題,沒有顯示我設置的背景顏色。我相信這是由於風格:UITableViewCellStyleSubtitle。

在其他情況下,偶然發現了這個問題,我相信這樣做的更好的解決方案在這一問題中發現:

UITableViewCellStyleSubtitle的BACKGROUNDCOLOR標籤? BackgroundColor of UITableViewCellStyleSubtitle labels?

答案在這裏轉載:

要更改表格視圖單元格的背景顏色,你需要設置它的tableView:willDisplayCell:forRowAtIndexPath:不是的tableView:的cellForRowAtIndexPath:否則將無法有任何影響,例如:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = [UIColor whiteColor]; 
} 
0

我覺得最好從自定義單元格的.m文件而不是tableViewController中調整顏色。看起來更直觀,而且更容易一些。只要調整顏色根據需要在方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 

注:請確保您調用這些方法中的超級得在呼叫,例如結束如果你想使你的綠色觸摸:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.backgroundColor = [UIColor greenColor]; 
    [super touchesBegan:touches withEvent:event]; 
} 

而且,以我的經驗,這是不值得調整touchesEnded的顏色,因爲你的細胞往往不會及時調整快速選曲用戶。

0

你應該總是提供willDisplayCell細胞的變化:forRowAtIndex代替cellForRowAtIndex方法,按照蘋果的文檔

這個作品!

0

改變顏色下面的委託方法:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (...){ 
     cell.backgroundColor = [UIColor blueColor]; 
    } else { 
     cell.backgroundColor = [UIColor whiteColor]; 
    } 
}