I’m developing an iPhone Application, and I have a small issue, or so I think. I have a tableview and I got it to set the colors and labels of the cells by returning the cells in a method. Everything seems fine when I run it, the problem is when I scroll the cell’s color changes to that of the cell below or above. I’m not sure how to solve this. I believe it’s updating the cell when I scroll for some reason. How would I change this to only set the cell properties at first and not when I scroll?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
tableCellCount ++;
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
UIColor * lightRed = [UIColor colorWithRed:0.6 green:0.1 blue:0 alpha:1.0];
UIColor * darkRed = [UIColor colorWithRed:0.7 green:0.1 blue:0 alpha:1.0];
UIColor * darkGray = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1.0];
NSLog(@"A");
if (tableCellCount % 2 == 0) {
cell.contentView.backgroundColor = darkRed;
cell.detailTextLabel.backgroundColor = darkRed;
cell.textLabel.backgroundColor = darkRed;
} else {
cell.contentView.backgroundColor = lightRed;
cell.detailTextLabel.backgroundColor = lightRed;
cell.textLabel.backgroundColor = lightRed;
}
cell.textLabel.textColor = [UIColor whiteColor];
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:darkGray];
[cell setSelectedBackgroundView:bgColorView];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
Update
I figured out the solution to my problem.
Here’s the updated code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[[content objectAtIndex:indexPath.section] objectForKey:@"rowValues"]
objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"News_Icon@2x.png"];
UIColor * lightRed = [UIColor colorWithRed:0.7 green:0.1 blue:0 alpha:1.0];
UIColor * darkRed = [UIColor colorWithRed:0.6 green:0.1 blue:0 alpha:1.0];
UIColor * darkGray = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1.0];
if (row % 2 == 0) {
cell.contentView.backgroundColor = darkRed;
cell.detailTextLabel.backgroundColor = darkRed;
cell.textLabel.backgroundColor = darkRed;
} else {
cell.contentView.backgroundColor = lightRed;
cell.detailTextLabel.backgroundColor = lightRed;
cell.textLabel.backgroundColor = lightRed;
}
cell.textLabel.textColor = [UIColor whiteColor];
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:darkGray];
[cell setSelectedBackgroundView:bgColorView];
for(UIView *view in [tableView subviews]) {
if([[[view class] description] isEqualToString:@"UITableViewIndex"]) {
[view setBackgroundColor:[UIColor clearColor]];
}
}
return cell;
}
For each cell in the table, there are properties you want to set the first time only when the cell gets created and there are other properties you want to set each time.