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 {}@endIf you need more code, shout. :)Many thanks in advance,Nick 解决方案 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). 这篇关于以编程方式选择时 UITableViewCell 变黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 07:29