2010-06-24 4 views
1

사용자가 스크롤 한 후 내 UITableViewCell이 반복되는 이유를 모르겠습니다.스크롤 후 UITableViewCell이 반복됩니다.

-(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 블록을 주석 처리하면 돈을

- (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; 
    } 

이 세포가 생성되는 방식입니다 더 이상 나타나지 않습니다. 왜 그런지 모르겠지만 여전히 해상도를 찾고 싶습니다.

업데이트 # 2 messageLabel 만 반복됩니다. 다른 모든 레이블은 반복되지 않습니다. 왜 이런거야?

답변

3

올바른 메모리 관리를 위해 테이블 ​​셀을 다시 사용하기 때문에 dequeueReusableCellWithIdentifier :)을 통해 수행해야합니다. 이렇게하면 필요한 모든 단일 셀에 대해 고유 한 셀이 없습니다. 대신 스크롤하는 동안 약 5 ~ 6 개 정도만 다시 사용됩니다.

셀이 다시 볼 때 어떤 셀이 표시 되든 관계없이 각 셀의 상태 (예 : 정보, 레이블, 이미지 등)를 재설정해야합니다.

+0

상태를 재설정해야하는 위치의 예를 보여줄 수 있습니까? –

+0

im 아래에있는 셀을 업데이트하는 중임 –

+1

'drawRect' 또는 XIB를 통해 셀을 만드는 방법에 따라 실제로 사과가 어떻게되는지 보길 원할 수 있습니다. http://developer.apple.com/iphone/library/samplecode/AdvancedTableViewCells/Introduction/Intro.html – iwasrobbed

0

당신이 할 수있는 한 가지 방법은 셀을 생성하거나 큐에서 대기열에 넣은 다음 cell.contentView의 모든 뷰를 제거하는 것입니다. 또는 아래로 스크롤 할 때 다른 값으로 설정할 수있는 레이블의 특성을 사용하여 사용자 지정 셀을 만들 수 있습니다.

관련 문제