2012-08-27 5 views
0

나는 uitableview가 있고 각 행에 이미지를 할당했습니다. tableView는 채팅 메시지 및 채팅 거품 이미지를 표시합니다 (ios 메시징 앱과 동일).uitableview를 스크롤 할 때 이미지 복제

스크롤 속도가 매우 느려지고 이미지가 위아래로 스크롤되는 것과 같이 복제되는 것처럼 보입니다.

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

    NSLog(@"tableView method called AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 

    //static NSString *CellIdentifier = @"Cell"; 

    //---add this--- 
    UILabel* dateLabel = nil; 
    UILabel* messageLabel = nil; 
    UIImageView *imageView_top_left = nil; 
    UIImageView *imageView_top_middle = nil; 
    UIImageView *imageView_top_right = nil; 

    UIImageView *imageView_middle_left = nil; 
    UIImageView *imageView_middle_right = nil; 
    UIImageView *imageView_middle_middle = nil; 

    UIImageView *imageView_bottom_left = nil; 
    UIImageView *imageView_bottom_middle = nil; 
    UIImageView *imageView_bottom_right = nil; 


    /*UITableViewCell *cell = [tableView 
           dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 


    }*/ 


    ChatMessage *chatMessage = [self.chatMessagesList objectAtIndex:indexPath.row]; 




    // Check whether this message is to me 

    if (![chatMessage.fromId isEqualToString:myId]){ 

     /////////// changed ////////// 
     static NSString *CellIdentifier = @"Cell1"; 

     UITableViewCell *cell = [tableView 
           dequeueReusableCellWithIdentifier:CellIdentifier]; 


     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     //////////////////////////// 

     NSLog(@"AAAAAAAAAAAAAAAAA"); 

     /////////////////// image //////////////////////////////////////////////////////////// 

     NSURL *avatarUrl = [NSURL URLWithString: 
          @"http://www.mobyn.com/mobyn_v3/application/upload/profile_images/default.jpg"]; 
     UIImage *avatarImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:avatarUrl]]; 

     UIImageView *avatar = [[UIImageView alloc] initWithImage:avatarImage]; 
     avatar.frame = CGRectMake(0, 23, 60, 60); 
     [cell.contentView addSubview:avatar]; 

     //[avatarImage release]; 
     //[avatar release]; 

     ////////////////////////////////////////////////////////////////////////////////////// 


     // If YES, then create the row with the corresponding bubble 
     //---add this--- 
     //---date--- 
     // dateLabel = [[[UILabel alloc] init] autorelease]; 
     //dateLabel.tag = DATELABEL_TAG; 
     //[cell.contentView addSubview: dateLabel]; 

     //---top left--- 
     imageView_top_left = [[[UIImageView alloc] init] autorelease]; 
     imageView_top_left.tag = IMAGEVIEW_TAG_1;   
     [cell.contentView addSubview: imageView_top_left]; 

     //---top middle--- 
     imageView_top_middle = [[[UIImageView alloc] init] autorelease]; 
     imageView_top_middle.tag = IMAGEVIEW_TAG_2; 
     [cell.contentView addSubview: imageView_top_middle]; 

     //---top right--- 
     imageView_top_right = [[[UIImageView alloc] init] autorelease]; 
     imageView_top_right.tag = IMAGEVIEW_TAG_3; 
     [cell.contentView addSubview: imageView_top_right]; 

     //---middle left--- 
     imageView_middle_left = [[[UIImageView alloc] init] autorelease]; 
     imageView_middle_left.tag = IMAGEVIEW_TAG_4; 
     [cell.contentView addSubview: imageView_middle_left]; 

     //---middle middle--- 
     imageView_middle_middle = [[[UIImageView alloc] init] autorelease]; 
     imageView_middle_middle.tag = IMAGEVIEW_TAG_5; 
     [cell.contentView addSubview: imageView_middle_middle]; 

     //---middle right--- 
     imageView_middle_right = [[[UIImageView alloc] init] autorelease]; 
     imageView_middle_right.tag = IMAGEVIEW_TAG_6; 
     [cell.contentView addSubview: imageView_middle_right]; 

     //---bottom left--- 
     imageView_bottom_left = [[[UIImageView alloc] init] autorelease]; 
     imageView_bottom_left.tag = IMAGEVIEW_TAG_7; 
     [cell.contentView addSubview: imageView_bottom_left]; 

     //---bottom middle--- 
     imageView_bottom_middle = [[[UIImageView alloc] init] autorelease]; 
     imageView_bottom_middle.tag = IMAGEVIEW_TAG_8; 
     [cell.contentView addSubview: imageView_bottom_middle]; 

     //---bottom right--- 
     imageView_bottom_right = [[[UIImageView alloc] init] autorelease]; 
     imageView_bottom_right.tag = IMAGEVIEW_TAG_9; 
     [cell.contentView addSubview: imageView_bottom_right]; 

     //---message--- 
     messageLabel = [[[UILabel alloc] init] autorelease]; 
     messageLabel.tag = MESSAGELABEL_TAG;   
     [cell.contentView addSubview: messageLabel]; 

     //---set the images to display for each UIImageView--- 
     imageView_top_left.image = 
     [UIImage imageNamed:@"bubble_top_left2.png"]; 
     imageView_top_middle.image = 
     [UIImage imageNamed:@"bubble_top_middle2.png"]; 
     imageView_top_right.image = 
     [UIImage imageNamed:@"bubble_top_right2.png"]; 

     imageView_middle_left.image = 
     [UIImage imageNamed:@"bubble_middle_left2.png"]; 
     imageView_middle_middle.image = 
     [UIImage imageNamed:@"bubble_middle_middle2.png"]; 
     imageView_middle_right.image = 
     [UIImage imageNamed:@"bubble_middle_right2.png"]; 

     imageView_bottom_left.image = 
     [UIImage imageNamed:@"bubble_bottom_left2.png"]; 
     imageView_bottom_middle.image = 
     [UIImage imageNamed:@"bubble_bottom_middle2.png"]; 
     imageView_bottom_right.image = 
     [UIImage imageNamed:@"bubble_bottom_right2.png"]; 


     /////////////////////////////////////////////// 
     //---calculate the height for the label--- 
     int labelHeight = [self labelHeight:messsageForTheRow]; 
     labelHeight -= bubbleFragment_height; 
     if (labelHeight<0) labelHeight = 0; 

     //---you can customize the look and feel for the date for each message here--- 
     dateLabel.frame = CGRectMake(0.0, 0.0, 200, 15.0); 
     dateLabel.font = [UIFont boldSystemFontOfSize: FONTSIZE]; 
     dateLabel.textAlignment = UITextAlignmentLeft; 
     dateLabel.textColor = [UIColor darkGrayColor]; 
     dateLabel.backgroundColor = [UIColor clearColor]; 

     //---top left--- 
     imageView_top_left.frame = 
     CGRectMake(bubble_x, bubble_y, bubbleFragment_width, bubbleFragment_height);   
     //---top middle--- 
     imageView_top_middle.frame = 
     CGRectMake(bubble_x + bubbleFragment_width, bubble_y, 
        bubbleFragment_width, bubbleFragment_height);   
     //---top right--- 
     imageView_top_right.frame = 
     CGRectMake(bubble_x + (bubbleFragment_width * 2), bubble_y, 
        bubbleFragment_width, bubbleFragment_height);   
     //---middle left--- 
     imageView_middle_left.frame = 
     CGRectMake(bubble_x, bubble_y + bubbleFragment_height, 
        bubbleFragment_width, labelHeight);   
     //---middle middle--- 
     imageView_middle_middle.frame = 
     CGRectMake(bubble_x + bubbleFragment_width, bubble_y + bubbleFragment_height, 
        bubbleFragment_width, labelHeight);   
     //---middle right--- 
     imageView_middle_right.frame = 
     CGRectMake(bubble_x + (bubbleFragment_width * 2), 
        bubble_y + bubbleFragment_height, 
        bubbleFragment_width, labelHeight);   
     //---bottom left--- 
     imageView_bottom_left.frame = 
     CGRectMake(bubble_x, bubble_y + bubbleFragment_height + labelHeight, 
        bubbleFragment_width, bubbleFragment_height);   
     //---bottom middle--- 
     imageView_bottom_middle.frame = 
     CGRectMake(bubble_x + bubbleFragment_width, 
        bubble_y + bubbleFragment_height + labelHeight, 
        bubbleFragment_width, bubbleFragment_height);   
     //---bottom right--- 
     imageView_bottom_right.frame = 
     CGRectMake(bubble_x + (bubbleFragment_width * 2), 
        bubble_y + bubbleFragment_height + labelHeight, 
        bubbleFragment_width, bubbleFragment_height); 

     //---you can customize the look and feel for each message here---  
     messageLabel.frame = 
     CGRectMake(bubble_x + 10, bubble_y + 5, 
        (bubbleFragment_width * 3) - 25, 
        (bubbleFragment_height * 2) + labelHeight - 10); 

     messageLabel.font = [UIFont systemFontOfSize:FONTSIZE];   
     messageLabel.textAlignment = UITextAlignmentCenter; 
     messageLabel.textColor = [UIColor darkTextColor]; 
     messageLabel.numberOfLines = 0; //---display multiple lines--- 
     messageLabel.backgroundColor = [UIColor clearColor]; 
     messageLabel.lineBreakMode = UILineBreakModeWordWrap; 

     ///// changed /////// 
     messsageForTheRow = chatMessage.messsage; 
     messageLabel.text = messsageForTheRow ; 
     return cell; 
     //////////////////// 
    } else { 

     /////////// changed ////////// 
     static NSString *CellIdentifier = @"Cell2"; 

     UITableViewCell *cell = [tableView 
           dequeueReusableCellWithIdentifier:CellIdentifier]; 


     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     //////////////////////////// 

     // If NO, then create the row with the bubble which shows it from me 
     // If YES, then create the row with the corresponding bubble 
     //---add this--- 
     //---date--- 
     // dateLabel = [[[UILabel alloc] init] autorelease]; 
     //dateLabel.tag = DATELABEL_TAG; 
     //[cell.contentView addSubview: dateLabel]; 

     /////////////////// image //////////////////////////////////////////////////////////// 

     NSURL *avatarUrl = [NSURL URLWithString: 
          @"http://www.mobyn.com/mobyn_v3/application/upload/profile_images/default.jpg"]; 
     UIImage *avatarImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:avatarUrl]]; 

     UIImageView *avatar = [[UIImageView alloc] initWithImage:avatarImage]; 
     avatar.frame = CGRectMake(255, 23, 60, 60); 
     [cell.contentView addSubview:avatar]; 

     //[avatarImage release]; 
     //[avatar release]; 

     ////////////////////////////////////////////////////////////////////////////////////// 


     NSLog(@"BBBBBBBBBBBBBBBBB"); 


     //---top left--- 
     imageView_top_left = [[[UIImageView alloc] init] autorelease]; 
     imageView_top_left.tag = IMAGEVIEW_TAG_1;   
     [cell.contentView addSubview: imageView_top_left]; 

     //---top middle--- 
     imageView_top_middle = [[[UIImageView alloc] init] autorelease]; 
     imageView_top_middle.tag = IMAGEVIEW_TAG_2; 
     [cell.contentView addSubview: imageView_top_middle]; 

     //---top right--- 
     imageView_top_right = [[[UIImageView alloc] init] autorelease]; 
     imageView_top_right.tag = IMAGEVIEW_TAG_3; 
     [cell.contentView addSubview: imageView_top_right]; 

     //---middle left--- 
     imageView_middle_left = [[[UIImageView alloc] init] autorelease]; 
     imageView_middle_left.tag = IMAGEVIEW_TAG_4; 
     [cell.contentView addSubview: imageView_middle_left]; 

     //---middle middle--- 
     imageView_middle_middle = [[[UIImageView alloc] init] autorelease]; 
     imageView_middle_middle.tag = IMAGEVIEW_TAG_5; 
     [cell.contentView addSubview: imageView_middle_middle]; 

     //---middle right--- 
     imageView_middle_right = [[[UIImageView alloc] init] autorelease]; 
     imageView_middle_right.tag = IMAGEVIEW_TAG_6; 
     [cell.contentView addSubview: imageView_middle_right]; 

     //---bottom left--- 
     imageView_bottom_left = [[[UIImageView alloc] init] autorelease]; 
     imageView_bottom_left.tag = IMAGEVIEW_TAG_7; 
     [cell.contentView addSubview: imageView_bottom_left]; 

     //---bottom middle--- 
     imageView_bottom_middle = [[[UIImageView alloc] init] autorelease]; 
     imageView_bottom_middle.tag = IMAGEVIEW_TAG_8; 
     [cell.contentView addSubview: imageView_bottom_middle]; 

     //---bottom right--- 
     imageView_bottom_right = [[[UIImageView alloc] init] autorelease]; 
     imageView_bottom_right.tag = IMAGEVIEW_TAG_9; 
     [cell.contentView addSubview: imageView_bottom_right]; 

     //---message--- 
     messageLabel = [[[UILabel alloc] init] autorelease]; 
     messageLabel.tag = MESSAGELABEL_TAG;   
     [cell.contentView addSubview: messageLabel]; 

     //---set the images to display for each UIImageView--- 
     imageView_top_left.image = 
     [UIImage imageNamed:@"bubble_top_left.png"]; 
     imageView_top_middle.image = 
     [UIImage imageNamed:@"bubble_top_middle.png"]; 
     imageView_top_right.image = 
     [UIImage imageNamed:@"bubble_top_right.png"]; 

     imageView_middle_left.image = 
     [UIImage imageNamed:@"bubble_middle_left.png"]; 
     imageView_middle_middle.image = 
     [UIImage imageNamed:@"bubble_middle_middle.png"]; 
     imageView_middle_right.image = 
     [UIImage imageNamed:@"bubble_middle_right.png"]; 

     imageView_bottom_left.image = 
     [UIImage imageNamed:@"bubble_bottom_left.png"]; 
     imageView_bottom_middle.image = 
     [UIImage imageNamed:@"bubble_bottom_middle.png"]; 
     imageView_bottom_right.image = 
     [UIImage imageNamed:@"bubble_bottom_right.png"]; 

     ///////////////////////////////////////////////// 
     //---calculate the height for the label--- 
     int labelHeight = [self labelHeight:messsageForTheRow]; 
     labelHeight -= bubbleFragment_height; 
     if (labelHeight<0) labelHeight = 0; 

     //---you can customize the look and feel for the date for each message here--- 
     dateLabel.frame = CGRectMake(0.0, 0.0, 200, 15.0); 
     dateLabel.font = [UIFont boldSystemFontOfSize: FONTSIZE]; 
     dateLabel.textAlignment = UITextAlignmentLeft; 
     dateLabel.textColor = [UIColor darkGrayColor]; 
     dateLabel.backgroundColor = [UIColor clearColor]; 

     //---top left--- 
     imageView_top_left.frame = 
     CGRectMake(bubble_x2, bubble_y, bubbleFragment_width, bubbleFragment_height);   
     //---top middle--- 
     imageView_top_middle.frame = 
     CGRectMake(bubble_x2 + bubbleFragment_width, bubble_y2, 
        bubbleFragment_width, bubbleFragment_height);   
     //---top right--- 
     imageView_top_right.frame = 
     CGRectMake(bubble_x2 + (bubbleFragment_width * 2), bubble_y2, 
        bubbleFragment_width, bubbleFragment_height);   
     //---middle left--- 
     imageView_middle_left.frame = 
     CGRectMake(bubble_x2, bubble_y2 + bubbleFragment_height, 
        bubbleFragment_width, labelHeight);   
     //---middle middle--- 
     imageView_middle_middle.frame = 
     CGRectMake(bubble_x2 + bubbleFragment_width, bubble_y2 + bubbleFragment_height, 
        bubbleFragment_width, labelHeight);   
     //---middle right--- 
     imageView_middle_right.frame = 
     CGRectMake(bubble_x2 + (bubbleFragment_width * 2), 
        bubble_y2 + bubbleFragment_height, 
        bubbleFragment_width, labelHeight);   
     //---bottom left--- 
     imageView_bottom_left.frame = 
     CGRectMake(bubble_x2, bubble_y2 + bubbleFragment_height + labelHeight, 
        bubbleFragment_width, bubbleFragment_height);   
     //---bottom middle--- 
     imageView_bottom_middle.frame = 
     CGRectMake(bubble_x2 + bubbleFragment_width, 
        bubble_y2 + bubbleFragment_height + labelHeight, 
        bubbleFragment_width, bubbleFragment_height);   
     //---bottom right--- 
     imageView_bottom_right.frame = 
     CGRectMake(bubble_x2 + (bubbleFragment_width * 2), 
        bubble_y2 + bubbleFragment_height + labelHeight, 
        bubbleFragment_width, bubbleFragment_height); 

     //---you can customize the look and feel for each message here---  
     messageLabel.frame = 
     CGRectMake(bubble_x2 + 10, bubble_y2 + 5, 
        (bubbleFragment_width * 3) - 25, 
        (bubbleFragment_height * 2) + labelHeight - 10); 

     messageLabel.font = [UIFont systemFontOfSize:FONTSIZE];   
     messageLabel.textAlignment = UITextAlignmentCenter; 
     messageLabel.textColor = [UIColor darkTextColor]; 
     messageLabel.numberOfLines = 0; //---display multiple lines--- 
     messageLabel.backgroundColor = [UIColor clearColor]; 
     messageLabel.lineBreakMode = UILineBreakModeWordWrap; 

     ///// changed /////// 
     messsageForTheRow = chatMessage.messsage; 
     messageLabel.text = messsageForTheRow ; 
     return cell; 
     //////////////////// 

    } 

    // Then set the data 


    messsageForTheRow = chatMessage.messsage; 
    messageLabel.text = messsageForTheRow ;  

    //-------------- 

    /////////// changed ////////// 
    UITableViewCell *cell = [UITableViewCell alloc]; 
    return cell; 
} 

답변

0

당신은 당신의 IF 문에서이 코드를 둘 필요가

//---add this--- 
UILabel* dateLabel = nil; 
UILabel* messageLabel = nil; 
UIImageView *imageView_top_left = nil; 
UIImageView *imageView_top_middle = nil; 
UIImageView *imageView_top_right = nil; 

UIImageView *imageView_middle_left = nil; 
UIImageView *imageView_middle_right = nil; 
UIImageView *imageView_middle_middle = nil; 

UIImageView *imageView_bottom_left = nil; 
UIImageView *imageView_bottom_middle = nil; 
UIImageView *imageView_bottom_right = nil; 

이 문서가 문제를 해결하는 희망

0

당신은 아마 cellForRowAtIndexPath에 chatMessage 초기화/할당을-호출을 다시하고 있습니다.

기본적으로 UITableView은 위/아래로 스크롤 할 때 오프 스크린 셀 데이터를 해제합니다. 다시 스크롤하면 cellForRowAtIndexPath가 다시 호출됩니다.

  • 다시

앱이해야 다시 수 아래로

  • 스크롤 중단 점을 비활성화 다시
  • 스크롤을 cellForRowAtIndexPath
  • 에 중단 점을 설정 시도

    cellForRowAtIndexPath에서 휴식.

    내 생각에 계속 UITableViewCell *cell = [UITableViewCell alloc];으로 전화하시는 것입니다.

    UITableView 위아래로 스크롤 할 때마다 cellForRowAtIndexPath이 호출되어 셀 할당을 다시 호출하므로 결국 여러 이미지가 표시됩니다.

    UITableViewCell *cell = [tableView 
              dequeueReusableCellWithIdentifier:CellIdentifier]; 
    
    if (cell == nil) { 
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    

    이동 직후 :

  • 관련 문제