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

それは完璧に男を働かせました!ありがとう! :) –

+0

私は助けることができてうれしい –

0

は、新しいクラスを作成

、あなたの細胞のカスタマイズに完全にコントロールを持っている私のためcell.backgroundColor=[UIColor blueColor];

+0

その動作していない人を使用することができます...多分あなたは別の解決策がありますか? –

+0

@YossiTsafar申し訳ありませんが、私の編集をチェックしてください – Daniel

2

を働いていた、私はセルの表示を無効にするために、より良い好む、細胞のbackgroundColorプロパティを設定してみてくださいUITableViewによって呼び出されるあなたの細胞ビュー、ため、

後、私は私の仕事用のコンピュータを取得するとき、あなたの答えを見つけたhaventは場合、私はいくつかのサンプルコードを掲載します、

あなたが同様にあなたのセルの背景用の画像を配置し、あなたのセルのカスタムの場所で異なるラベルや画像、ボタン、テキストフィールドを配置することができ

あなたはそれがどのように動作するかを見たら210は

EDIT、非常に簡単です>>

コード! [背景を変更するためにやり過ぎばかりですが、あなたが本当にあなたのセルをカスタマイズしたい場合は、これが移動するための方法です!]あなたの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

男、ありがとう! –

+0

@ YossiTsafar check edit!、image.pngを背景に、私はより柔軟だと思います。 – MaKo

+0

ありがとうございました! :) –

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は何の違いもなく、(私のように)混乱してラウンドします。 3迅速で

0
cell.backgroundColor = [UIColor redColor]; 
0

あなたは

cell.backgroundcolor = UIColor.white 
関連する問題