UITableViewCell无法正确更新

Dav*_*vid 1 iphone objective-c uitableview ios

我已经创建了一个自定义的UITableViewCell,但是在更新单元格的内容时遇到了问题.当表中有多个单元格时,表格不会在单元格中绘制正确的图像.每个单元格中的图像应该是唯一的,但是我看到具有相同图像的不同单元格.该表似乎是随机放置细胞.

我用NSLog检查了我的数据源,名称是正确的.我可以在不使用时更正问题- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier,而是每次都创建一个新单元格- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath.

关于我可能做错什么的任何建议?请看下面的代码.

- (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;
}
Run Code Online (Sandbox Code Playgroud)

ScoreCell.h

@interface ScoreCell : UITableViewCell
{
    UIImage *scoreImage;
}

@property(nonatomic, retain)UIImage *scoreImage;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Jef*_*ley 5

除了其他注释之外,请务必-prepareForReuse在您的ScoreCell类中实现该方法.当要重复使用单元格时会调用它,此时应清除图像.务必致电[super prepareForReuse];您的实施.这将防止单元格被错误的图像重复使用.