无法更改UITableViewCell背景颜色

时间:2012-02-02 19:46:31

标签: iphone objective-c ios xcode uitableview

UITableViewCell适合UITableView,但在进入编辑样式模式时,我无法改变背景颜色。我已经尝试了网上的所有内容,整天搜索,但仍然无法修复它(我已经搜索过StackOverflow)。拜托,帮助我。

- (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;
            }
        }
    }

9 个答案:

答案 0 :(得分:18)

尝试在tableView: willDisplayCell:方法中自定义单元格,如下所示:

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

    cell.backgroundColor = [UIColor redColor];

}

答案 1 :(得分:5)

您需要更改tableView单元格contentView,而不是单元格的背景视图。

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

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

答案 2 :(得分: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;

   }

享受!! ;)

答案 3 :(得分:0)

尝试设置单元格backgroundColor属性,为我工作cell.backgroundColor=[UIColor blueColor];

答案 4 :(得分:0)

请试试这个,它对我有用。

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

答案 5 :(得分: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];
    }

希望它可以帮助你们

答案 6 :(得分:0)

确保您尚未在故事板中设置内容视图的背景颜色。如果你这样做了,在代码中更改cell.backgroundColor将没有任何区别,你会在混乱中四处走动(就像我一样)。

答案 7 :(得分:0)

 cell.backgroundColor = [UIColor redColor];

答案 8 :(得分:0)

在swift 3中你可以使用

cell.backgroundcolor = UIColor.white
相关问题