2010-06-24 79 views
1

我不确定为什么我的UITableViewCell在用户滚动后重复?UITableViewCell滚动后重复

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"Cell"; 

    ChartlyCell *cell = (ChartlyCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     //cell = [[[ChartlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell = [[[ChartlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier andDelegate:self] autorelease]; 
    } 


    ChartlyObject *myChartlyObject; 
    myChartlyObject = [self.items objectAtIndex:indexPath.row]; 

    cell.usernameLabel.text = myChartlyObject.userName; 
    cell.bodyLabel.text = myChartlyObject.chart_message; 
    [cell.messageLabel setText: myChartlyObject.chart_message]; 
    cell.timeLabel.text = [Utils toShortTimeIntervalStringFromStockTwits: myChartlyObject.created_at]; 
    [cell.chartImage loadImageFromURL:[NSURL URLWithString:myChartlyObject.imageThumbnail]]; 

     return cell; 
    } 

这是细胞如何被创建:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andDelegate:(id<NLabelDelegate>)ndelegate{ 
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { 
     // Initialization code 
    CGRect rect; 

    rect = CGRectMake(77, 5, 200, 20); //CGRectMake(10, 5, 200, 20); 
    usernameLabel = [[UILabel alloc] initWithFrame:rect]; 
    //usernameLabel.backgroundColor = backgroundColor; 
    usernameLabel.font = [Utils getBoldSystemFontWithSize:14]; 
    usernameLabel.textColor = [UIColor colorWithRed:.368 green:.741 blue:.784 alpha:1]; 
    [self.contentView addSubview: usernameLabel]; 

    rect = CGRectMake(277, 5, 38, 20); 
    timeLabel = [[UILabel alloc] initWithFrame:rect]; 
    timeLabel.textAlignment = UITextAlignmentRight; 
    //timeLabel.backgroundColor = backgroundColor; 
    timeLabel.font = [Utils getBoldSystemFontWithSize:14]; 
    timeLabel.textColor = [UIColor colorWithRed:.98 green:.65 blue:.01 alpha:1]; 
    [self.contentView addSubview: timeLabel]; 

    /*rect = CGRectMake(77, 25, 238, 68); //CGRectMake(10, 50, 238, 68); 
    bodyLabel = [[UILabel alloc] initWithFrame:rect]; 
    bodyLabel.font = [Utils getSystemFontWithSize:10]; 
    bodyLabel.numberOfLines = 3; 
    bodyLabel.lineBreakMode = UILineBreakModeWordWrap; 
    bodyLabel.textColor = [UIColor blackColor]; 
    bodyLabel.backgroundColor = [UIColor clearColor]; 
    [self.contentView addSubview: bodyLabel];*/ 

    rect = CGRectMake(77, 25, 238, 68); 
    messageLabel = [[NLabel alloc] initWithFrame:rect andDelegate:ndelegate]; 
    [messageLabel setOpaque:NO]; 
    [messageLabel setBackgroundColor: [UIColor blackColor]]; 
    //[messageLabel setTotalheight:68]; 
    [self.contentView addSubview: messageLabel]; 

    rect = CGRectMake(13, 10, 48, 48); 
    chartImage = [[AsyncImageView alloc]initWithFrame:rect]; 
    [self.contentView addSubview: chartImage]; 
    } 
    return self; 
} 

UPDATE: 是什么奇怪的是,如果我取消了bodyLabel块注释掉messageLabel块的重复单元唐再也不会出现了。我不知道为什么,仍然想要找到一个分辨率

更新#2 只有messageLabel得到重复。所有其他标签不重复。为什么是这样?

回答

3

因为您正在通过dequeueReusableCellWithIdentifier :)重新使用表格单元进行正确的内存管理,所以您应该通过dequeueReusableCellWithIdentifier这样做:)这种方式对于您需要的每个单元格都没有独特的单元格。相反,你只有5或6个在滚动过程中被重复使用。

当单元格重新进入视图时,您需要根据任何单元格进入视图来重置每个单元格的状态(即更新信息,标签,图像等)。

+0

你能举一个我需要重置状态的例子吗? – 2010-06-24 19:57:49

+0

即时消息下的印象我正在更新的单元格,之后,如果块 – 2010-06-24 20:02:27

+1

你可能想看看苹果是如何做到这一点,因为它真的取决于你如何通过'drawRect'或XIB创建单元格。 http://developer.apple.com/iphone/library/samplecode/AdvancedTableViewCells/Introduction/Intro.html – iwasrobbed 2010-06-24 20:18:34

0

您可以做的一件事是在您创建单元格或将其取出之后,删除cell.contentView中的所有视图。或者,您可以创建一个具有标签属性的自定义单元格,当您向下滚动时可以将其设置为不同的值。