我创建了一个自定义的UITableViewCell,但在更新单元格内容时遇到了问题。当我在表格中有多个单元格时,表格未在单元格中绘制正确的图像。每个单元格中的图像应该是唯一的,但是我看到不同的单元格具有相同的图像。该表似乎是随机放置细胞。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;
}
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
这个类名不要jive - 我希望这是在这里发布snippits的神器 – bshirley
谢谢,这是一个神器。固定。 – David
表面随机性不可能是随机的 - 当一个单元格滚动出视图时,它会被重用。一个是从另一侧滚动,如果状态没有被你的代码正确重置,你会得到你看到的行为 – bshirley