2012-02-02 146 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