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 回答
0

你需要从你的 IF 语句中取出这段代码:

UITableViewCell *cell = [tableView 
                             dequeueReusableCellWithIdentifier:CellIdentifier];

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

紧随其后:

//---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;

希望它能解决你的问题

于 2013-06-05T22:36:41.337 回答