2010-06-24 80 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中的所有視圖。或者,您可以創建一個具有標籤屬性的自定義單元格,當您向下滾動時可以將其設置爲不同的值。