2010-08-26 9 views
6

Me preguntaba por qué este código me da UITableViewCells negro cuando configuro la propiedad seleccionada. Si hago eso, el contenido de la celda se vuelve completamente negro, y no tengo idea de por qué.UITableViewCell va negro cuando se selecciona programáticamente

Aquí está el código

// 
// TableViewAdapter.m 
// TableviewScanMode 
// 
// Created by Nick Overdijk on 8/26/10. 
// Copyright 2010 Nick Overdijk. All rights reserved. 
// 

#import "TableViewAdapter.h" 
#import "Model.h" 

@implementation TableViewAdapter 

@synthesize model; 

- (id) initWithModel: (Model*) model { 
    self = [super init]; 
    if(self != nil){ 
     self->model = [model retain]; 
    } 

    return self; 
} 

- (void) dealloc { 
    [model release]; 
    [super dealloc]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [[model cellData] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[[model cellData] objectAtIndex: section] count]; 
} 

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

    static NSString *CellIdentifier = nil; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [[[model cellData] objectAtIndex: indexPath.section] objectAtIndex: indexPath.row]; 

    if(indexPath.row == [[model currentSelected] row] && indexPath.section == [[model currentSelected] section]){ 
     cell.selected = YES; 
    } else { 
     cell.selected = NO; 
    } 

    return cell; 
} 

@end 

// 
// RootViewController.m 
// TableviewScanMode 
// 
// Created by Nick Overdijk on 8/24/10. 
// Copyright Nick Overdijk 2010. All rights reserved. 
// 

#import "RootViewController.h" 
#import "Model.h" 
#import "TableViewAdapter.h" 


@implementation RootViewController 


#pragma mark - 
#pragma mark View lifecycle 

- (void)viewDidLoad { 
    model = [[Model alloc] init]; 
    [model addObserver:self 
      forKeyPath:@"updatedIndexPaths" 
       options:NSKeyValueObservingOptionNew 
       context:NULL 
    ]; 

    [model startSelectionRotation]; 

    adapter = [[TableViewAdapter alloc] initWithModel: model]; 
    self.tableView.dataSource = adapter; 

    [super viewDidLoad]; 
} 

- (void)dealloc { 
    [adapter release]; 
    [model release]; 
    [super dealloc]; 
} 

#pragma mark - 
#pragma mark KVO updates 
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    NSArray * reloadThese = [change objectForKey: NSKeyValueChangeNewKey]; 
    [self.tableView reloadRowsAtIndexPaths: reloadThese withRowAnimation: UITableViewRowAnimationFade]; 
} 

#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
} 


@end 

Si necesita más código, grito. :)

Muchas gracias de antemano, Nick

+0

¿Tiene una solución para esta? Tengo el mismo problema ... – meersmans

Respuesta

15

que estaba teniendo el mismo problema y lo arreglaron moviendo el cell.selected = YES en tableView:willDisplayCell:forRowAtIndexPath lugar.

Creo que podría estar relacionado con la nota en la parte inferior de los documentos UITableViewCell sobre los cambios en el color de fondo que requieren el uso de tableView: willDisplayCell: forRowAtIndexPath (presumiblemente selected establece el color de fondo).

+0

¡Comprobaré esto cuando tenga la oportunidad! Muchas gracias. – Nick

+0

¡GRACIAS! me ahorró un montón de tiempo de excavar en la documentación. – Joe

Cuestiones relacionadas