2012-02-02 22 views
5

Tengo UITableViewCell que se adapta a UITableView, pero de alguna manera no puedo cambiar el color de fondo al ingresar al modo de estilo de edición .. He intentado todo en la web, búsqueda todo el día, pero todavía no se pudo arreglar (busqué StackOverflow como ... bueno). por favor, ayúdame.no puede cambiar el color de fondo 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; 
      } 
     } 
    } 

Respuesta

18

Intente personalizar el interior de la célula tableView: willDisplayCell: método, algo como esto:

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

    cell.backgroundColor = [UIColor redColor]; 

} 
+0

¡funciona perfectamente hombre! ¡Gracias! :) –

+0

me alegro de poder ayudar –

0

el establecimiento de las células intento propiedad backgroundColor, trabajado para mí cell.backgroundColor=[UIColor blueColor];

+0

su hombre no trabaja .. ¿quién no tiene otra solución? –

+0

@YossiTsafar lo siento, mira mi edición – Daniel

2

tener un control total sobre la personalización de sus células, prefiero mejor para anular el punto de vista celular,

crea una nueva clase para su vista de celda, que recibe la llamada de UITableView,

más adelante cuando llego a mi computadora de trabajo si no ha encontrado su respuesta, publicaré un código de muestra,

una vez que vea cómo funciona es bastante fácil

usted podría así colocar una imagen para el fondo de la celda, y colocar diferentes etiquetas e imágenes, botones, campos de texto en los lugares de encargo de su celular,

EDITAR >>

el código! [Es un exceso sólo para cambiar el fondo, pero si desea personalizar realmente su celular, este es el camino a seguir!]

en su 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 

en su 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 

Y en su 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; 

    } 

ENJOY !! ;)

+0

ok hombre, gracias! –

+0

@YossiTsafar check edit !, con image.png como fondo, creo que es más flexible, – MaKo

+0

Muchas gracias hombre !!! :) –

0

Por favor, intente esto, trabaja para mí.

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

necesita cambiar contentView de la célula tableView, la opinión del fondo no es el de la célula.

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

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

As @Westley mencionado;

Debe cambiar el contentView de la celda tableView, no la vista de fondo de la celda.

Esta es la forma en que debe hacerlo:

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

creo que sirve ustedes por

0

Asegúrese de que ya no estableció de la vista de contenido color de fondo en el guión gráfico. Si lo hiciste, cambiando la celda.backgroundColor en el código no hará ninguna diferencia, y darás vueltas y vueltas en confusión (como lo hice yo).

0
cell.backgroundColor = [UIColor redColor]; 
0

en Swift 3 puede utilizar

cell.backgroundcolor = UIColor.white 
Cuestiones relacionadas