0

uitableview があり、各行に画像を割り当てました。tableView には、チャット メッセージとチャット バブルの画像が表示されます (iOS メッセージング アプリのように)。

スクロールが非常に遅く、上下にスクロールすると画像が重複しているように見えます。

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

2 に答える 2

0

おそらく、 で chatMessage の初期化/割り当てを再呼び出ししていますcellForRowAtIndexPath

デフォルトでUITableViewは、上下にスクロールすると画面外のセル データが解放されます。cellForRowAtIndexPathスクロールバックすると再度呼び出されます。

試す:

  • にブレークポイントを設定するcellForRowAtIndexPath
  • 無効にする
  • 下へスクロール
  • ブレークポイントを再度有効にする
  • もう一度上にスクロール

アプリは で壊れるはずですcellForRowAtIndexPath

私の推測では、あなたは電話をかけ続けていると思いますUITableViewCell *cell = [UITableViewCell alloc];

UITableView呼び出しを上下にスクロールするたびにcellForRowAtIndexPath、セル割り当てが再呼び出しされるため、複数の画像が表示されます。

于 2012-08-27T22:32:29.087 に答える