2012-02-02 139 views
5

我有適合UITableView的UITableViewCell,但不知何故,當進入編輯樣式模式時,我無法更改背景顏色..我嘗試了一切,在網上搜索整天,但仍然無法修復(我已經搜索過StackOverflow)。請幫幫我。無法更改UITableViewCell背景顏色

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCell" owner:nil options:nil]; 

     for (UIView *view in views) { 
      if([view isKindOfClass:[UITableViewCell class]]) 
      { 
       cell = (MainTableViewCell *)view; 
      } 
     } 
    } 

回答

18

嘗試定製在細胞內tableView: willDisplayCell:方法,像這樣:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell.backgroundColor = [UIColor redColor]; 

} 
+0

它完美的工作!謝謝! :) – 2012-02-02 21:38:59

+0

很高興我能幫上忙 – 2012-02-02 21:40:26

0

嘗試設置細胞backgroundColor屬性,工作對我來說cell.backgroundColor=[UIColor blueColor];

+0

它不工作的人..也許你有另一種解決方案? – 2012-02-02 20:10:02

+0

@YossiTsafar對不起,查看我的編輯 – Daniel 2012-02-02 20:21:39

2

會對你的細胞的定製總量控制,我寧願更好地覆蓋小區視圖,

創建一個新類爲您的單元格視圖,被UITableView調用,

以後當我到達我的工作計算機如果您還沒有找到您的答案我會發布一些示例代碼,

一旦你看到它是如何工作是很容易的

你還可將圖像以你的背景,並把不同的標籤和圖像,按鈕,文本框在你的細胞中的自定義的地方,

編輯>>

該代碼! [是矯枉過正只是改變的背景下,但如果你想真正自定義您的細胞,這是要走的路!]

在CustomCell.h

#import <UIKit/UIKit.h> 
@interface CustomCell : UITableViewCell { 
UILabel *_kLabel; 
UILabel *_dLabel; 
} 
@property (nonatomic, retain) UILabel *kLabel; 
@property (nonatomic, retain) UILabel *dLabel; 
- (void) initLabels; 
@end 

在CustomCell.m

#import "CustomCell.h" 
@implementation CustomCell 
@synthesize kLabel = _kLabel; 
@synthesize dLabel = _dLabel; 

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

    CGRect popUpImageBgndRect = CGRectMake(0, 0, 942, 44); 
    UIImageView *popUpImageBgnd = [[UIImageView alloc] initWithFrame:popUpImageBgndRect]; 
    [popUpImageBgnd setImage:[UIImage imageNamed:@"tableCellBgnd.png"]]; 
    popUpImageBgnd.opaque = YES; // explicitly opaque for performance 
    [self.contentView addSubview:popUpImageBgnd]; 
    [popUpImageBgnd release]; 

    [self initLabels]; 
    } 
    return self; 
    } 

    - (void)layoutSubviews { 

[super layoutSubviews]; 

CGRect contentRect = self.contentView.bounds; 

CGFloat boundsX = contentRect.origin.x; 

CGRect frame; 


frame= CGRectMake(boundsX+10 ,10, 200, 20); 

self.kLabel.frame = frame; 


frame= CGRectMake(boundsX+98 ,10, 100, 20); 

self.dLabel.frame = frame; 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
[super setSelected:selected animated:animated]; 

// Configure the view for the selected state 
} 

    - (void) initLabels { 
self.kLabel = [[[UILabel alloc]init] autorelease]; 
self.kLabel.textAlignment = UITextAlignmentLeft; 
self.kLabel.backgroundColor = [UIColor clearColor]; 
self.kLabel.font = [UIFont fontWithName:@"FS Albert" size:16]; 
self.kLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1]; 

self.dLabel = [[[UILabel alloc]init] autorelease]; 
self.dLabel.textAlignment = UITextAlignmentLeft; 
self.dLabel.backgroundColor = [UIColor clearColor]; 
self.dLabel.font = [UIFont systemFontOfSize:16]; 
self.dLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1]; 

[self.contentView addSubview:self.kLabel]; 
[self.contentView addSubview:self.dLabel]; 

} 

-(void) dealloc { 

[_kLabel release]; 
[_dLabel release]; 

[super dealloc]; 
} 

    @end 

而在你ViewController.m

YourViewController.m 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 



CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 


    return cell; 

    } 

享受! ;)

+0

好男人,謝謝! – 2012-02-02 21:40:10

+0

@YossiTsafar check edit !,以image.png爲背景,我覺得比較靈活, – MaKo 2012-02-03 05:13:54

+0

非常感謝! :) – 2012-02-03 10:47:50

0

請試試這個,它爲我工作。

UIView *bgView = [[UIView alloc] initWithFrame:cell.frame]; 
bgView.backgroundColor = [UIColor greenColor]; 
cell.backgroundView = bgView; 
[bgView release]; 
4

您需要更改tableView單元格的contentView,而不是單元格的背景視圖。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell.contentView.backgroundColor = [UIColor redColor]; 
} 
0

正如@Westley提到的那樣;

您需要更改tableView單元格的contentView,而不是單元格的背景視圖。

這是你應該怎麼做:

if(index == conditionYouWant) 
    { 
     cell.contentView.backgroundColor = [UIColor whiteColor]; 
    } 
    else 
    { 
     cell.contentView.backgroundColor = [UIColor colorWithRed:0.925 green:0.933 blue:0.937 alpha:1.0]; 
    } 

希望它可以幫助你們走出

0

確保你沒有已經設置的內容視圖的背景色在故事板。如果你這樣做,改變單元格。代碼中的backgroundColor將沒有任何區別,並且你會混亂(像我一樣)。

0
cell.backgroundColor = [UIColor redColor]; 
0

在迅速3,你可以使用

cell.backgroundcolor = UIColor.white