目标 - C:在Scroll上更改UITableView内容

Ada*_*m G 6 objective-c uitableview ios quickblox

我正在使用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;
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑请看下面我的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
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

rma*_*ddy 5

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

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

此外,您必须避免每次使用单元格时反复添加子视图.您有代码可以创建并向单元格添加图像视图.但是你一直在不断添加新的图像视图.如果需要,只需添加一次.如果它已经存在,请使用新图像更新它,而不是添加新图像.


小智 0

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

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