2010-08-26 22 views
6

Mi chiedevo perché questo codice mi avrebbe dato UITableViewCells nero quando ho impostato la proprietà selezionata su ON. Se lo faccio, il contenuto della cella diventa completamente nero e non ho idea del perché.UITableViewCell diventa nero quando viene selezionato a livello di programmazione

Ecco il codice

// 
// 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 

Se avete bisogno di più codice, grido. :)

Molte grazie in anticipo, Nick

+0

Avete una soluzione per questo? Ho lo stesso problema ... – meersmans

risposta

15

ho avuto lo stesso problema e risolto spostando il cell.selected = YES in tableView:willDisplayCell:forRowAtIndexPath invece.

penso che potrebbe essere correlato alla nota in fondo la documentazione UITableViewCell sulle modifiche apportate al colore di sfondo che richiedono l'uso di tableView: willDisplayCell: forRowAtIndexPath (presumibilmente selected imposta il colore di sfondo).

+0

Lo controllerò quando ne avrò l'occasione! Molte grazie. – Nick

+0

GRAZIE! mi ha risparmiato un sacco di tempo per scavare nella documentazione. – Joe

Problemi correlati