【问题标题】:Objective - C: UITableView Content Changing on Scroll目标 - C:UITableView 内容在滚动时发生变化
【发布时间】:2015-09-08 18:28:35
【问题描述】:

我正在使用 QuickBlox 框架来构建聊天应用程序。目前,当聊天视图打开时,一切看起来都很棒。

但是,当用户开始上下滚动聊天记录时,一些单元格开始发生变化(例如,它们会显示应该放置在不同行中的图像)。

下面是我的 cellForRowAtIndexPath 代码,如果有人能告诉我我做错了什么

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

    QBChatMessage *message = [[ChatService shared] messagsForDialogId:self.dialog.ID][indexPath.row];

    if (message.attachments.count > 0) {

        ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier];

        [cell configureCellWithImage:message];
        cell.backgroundColor = [UIColor whiteColor];

        return cell;

    } else {

        ChatMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ChatMessageCellIdentifier];

        [cell configureCellWithMessage:message];            
        cell.backgroundColor = [UIColor whiteColor];

        return cell;
    }

}

编辑请看下面我的 ImageTableViewCell configureCellWithImage 方法:

- (void) configureCellWithImage:(QBChatMessage*)message {

    NSString *time = [message.dateSent timeAgoSinceNow];

    if ([QBSession currentSession].currentUser.ID == message.senderID) {

    // Message was sent by me

        NSData *imageData = [FTWCache objectForKey:[NSString stringWithFormat:@"%@", [message.attachments[0] valueForKey:@"ID"]]];

        if (imageData) {

        // image is already downloaded

            dispatch_async(dispatch_get_main_queue(), ^{

            UIImage *image = [UIImage imageWithData:imageData];
            UIImageView *cellImage = [[UIImageView alloc] init];

            [self.backgroundImageView setFrame:CGRectMake(320-155, 10, 140, 140)];

            cellImage.frame = CGRectMake(7, 7, 120, 120);
            [cellImage setContentMode:UIViewContentModeScaleAspectFill];

            cellImage.clipsToBounds = YES;
            cellImage.layer.cornerRadius = 5;
            cellImage.image = image;

            self.backgroundImageView.image = aquaBubble;
            [self.backgroundImageView addSubview:cellImage];
            [self.contentView addSubview:self.backgroundImageView];

            });
        } else {

    // downloads the image and displays as above

    }

    } else {

    // Message was sent by another user

        NSData *imageData = [FTWCache objectForKey:[NSString stringWithFormat:@"%@", [message.attachments[0] valueForKey:@"ID"]]];

        if (imageData) {

            dispatch_async(dispatch_get_main_queue(), ^{

            UIImage *image = [UIImage imageWithData:imageData];
            UIImageView *cellImage = [[UIImageView alloc] init];

            [self.backgroundImageView setFrame:CGRectMake(padding/2, padding+5, 140, 140)];

            cellImage.frame = CGRectMake(13, 7, 120, 120);
            [cellImage setContentMode:UIViewContentModeScaleAspectFill];
            cellImage.layer.cornerRadius = 5;
            cellImage.clipsToBounds = YES;
            cellImage.image = image;

            self.timeLabel.frame = CGRectMake(20, self.backgroundImageView.frame.size.height + 20, 80, 20);
            self.timeLabel.text = [NSString stringWithFormat:@"%@", time];
            [self.timeLabel setFont:[UIFont systemFontOfSize:10.0]];
            [self.timeLabel setTextColor:[UIColor blackColor]];

            [self.contentView addSubview:self.timeLabel];

            self.nameAndDateLabel.textAlignment = NSTextAlignmentLeft;

            QBUUser *sender = [ChatService shared].usersAsDictionary[@(message.senderID)];

            NSInteger loginForColor = [sender.login integerValue];
            loginForColor = loginForColor % 255;

            self.nameAndDateLabel.text = [NSString stringWithFormat:@"%@", sender.fullName];

            self.backgroundImageView.image = orangeBubble;
            [self.backgroundImageView addSubview:cellImage];
            [self.contentView addSubview:self.backgroundImageView];

            });

        } else {

          // downloads the image and displays as above
        }

    }
}

【问题讨论】:

  • 你需要显示两个configureCellWithXXX:方法。
  • ImageCellIdentifier 和 ChatMEssageCellIdentifier 是两个独立的 xib 吗?
  • 我添加了 configureCellWithImage 方法供您查看。谢谢!

标签: ios objective-c uitableview quickblox


【解决方案1】:

细胞被重复使用。因此,您必须始终设置/重置单元格的所有属性。

对于设置单元格属性的每个 if 语句,必须有一个 else 语句重置相同的属性 - 即使它只是清除值。

您还必须避免在每次使用单元格时一遍又一遍地添加子视图。您有创建图像视图并将其添加到单元格的代码。但是你不断地添加新的图像视图。如果需要,只需添加一次。如果它已经存在,请使用新图像进行更新,而不是添加新图像。

【讨论】:

  • 这是给我的 - 你提出的这两件事。
【解决方案2】:

错误应该在函数 configureCellWithImage 和 configureCellWithMessage 上。

我没有看到这些函数的代码,但我敢打赌你没有清理 configureCellWithMessage 上的图像内容。

【讨论】:

  • 我添加了 configureCellWithImage 方法供您查看。谢谢!
  • 如果你也可以显示 configureWithMessage 应该很棒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
相关资源
最近更新 更多