David
David

Reputation: 14414

UITableViewCell not updating properly

I've created a custom UITableViewCell but am having trouble updating the contents of the cell. When I have multiple cells in the table, the table is not drawing the correct images in the cell. The images in each cell should be unique, however I am seeing different cells with the same image. The table seems to be placing the cells at random.

I've checked my data source with NSLog and the names are correct. I can correct the issue when I don't use - (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier, but instead create a new cell each time in - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath.

Any suggestion on what I may be doing wrong? Please have a look at my code below.

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ScoreCell *cell = (ScoreCell *)[_tableView dequeueReusableCellWithIdentifier:@"CellID"];

    if (cell == nil) 
    {
        cell = [[[ScoreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellID"] autorelease];
    }

    BoxScore *boxScore = [_gameDayData objectAtIndex:indexPath.row];
    [cell setScoreImage:[UIImage imageNamed:boxScore.name]];

    return cell;
}

ScoreCell.h

@interface ScoreCell : UITableViewCell
{
    UIImage *scoreImage;
}

@property(nonatomic, retain)UIImage *scoreImage;

@end

ScoreCell.m

@implementation ScoreCell

@synthesize scoreImage;

- (void)dealloc
{
    [scoreImage release], scoreImage = nil;
}


- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    [scoreImage drawAtPoint:CGPointMake(5,5)];
}

@end

Upvotes: 1

Views: 2484

Answers (3)

Jeff Kelley
Jeff Kelley

Reputation: 19071

In addition to the other comments, be sure to implement the -prepareForReuse method in your ScoreCell class. It gets called when the cell is to be reused, at which point you should clear the image. Be sure to call [super prepareForReuse]; in your implementation. This will prevent the cell from being reused with the wrong image.

Upvotes: 5

bshirley
bshirley

Reputation: 8356

There are two non-related problems with your image handling. Scrolling off and on the screen will cause a cell to load the image twice (or a hundred times, depending on the user).

You want a

- (UIImage *)boxScoreImageForIndex:(NSInteger)index 

method to (lazy) load, hold on to, and provide the image for the cell.

You also don't want to use imageNamed:, in your case it will cause twice as much memory usage than needed. Use imageWithContentsOfFile: instead.

Upvotes: 3

Jonathan.
Jonathan.

Reputation: 55604

You are not clearing the previous image. When a cell is dequeued it is not dealloced.

So sometimes the image that was drawn on the cell the times before it show through in front of the new image.

In drawRect you need to clear everything.

Either do:

CGContextClearRect( context , [self bounds] );

Or set clearsContextBeforeDrawing on the cell when it is created.

Upvotes: 0

Related Questions