Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

uitableview - UITableViewCell going black when selected programmatically

I was wondering why this code would give me black UITableViewCells when I set the selected property ON. If I do that, the content of the cell turns completely black, and I have no idea why.

Here's the code

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

If you need more code, shout. :)

Many thanks in advance, Nick

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was having the same problem and fixed it by moving the cell.selected = YES into tableView:willDisplayCell:forRowAtIndexPath instead.

I think it might be related to the note at the bottom of the UITableViewCell docs about changes to background color requiring use of tableView:willDisplayCell:forRowAtIndexPath (presumably selected sets the background color).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...