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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With