Fernando Cervantes
Fernando Cervantes

Reputation: 2962

UITableView Cell Updates When I Scroll

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:@"[email protected]"];

    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;
}

Upvotes: 1

Views: 4282

Answers (3)

Dancreek
Dancreek

Reputation: 9544

You have a problem with cells being reused. UITableViews recycle cells. So if you are not correctly handling this in your tableview:cellForRowAtIndex: method you will get strange results when you scroll. Always assume that the cell you are setting up may already be setup as another cell.

Upvotes: 0

iDifferent
iDifferent

Reputation: 2210

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.

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:anyId];
if(cell==nil)
{
    // Properties to set the first time when the cell is created only
}
// Properties to set each time

return cell;

Upvotes: 3

Gabriel
Gabriel

Reputation: 3359

You are reusing cell views. Check the method in your table view controller tableView:cellForRowAtIndexPath:and you for sure are using dequeueReusableCellWithIdentifier:. You have to set color values to that reused cell.

Upvotes: 1

Related Questions