Ios 目标-C:UITableView在滚动上更改内容
我正在使用QuickBlox框架构建一个聊天应用程序。当前,当聊天视图打开时,一切看起来都很棒 但是,当用户开始上下滚动聊天历史记录时,一些单元格开始改变(例如,它们将显示一个应放在不同行中的图像) 下面是我的CellForRowatineXpath代码,如果有人能告诉我我做错了什么Ios 目标-C:UITableView在滚动上更改内容,ios,objective-c,uitableview,quickblox,Ios,Objective C,Uitableview,Quickblox,我正在使用QuickBlox框架构建一个聊天应用程序。当前,当聊天视图打开时,一切看起来都很棒 但是,当用户开始上下滚动聊天历史记录时,一些单元格开始改变(例如,它们将显示一个应放在不同行中的图像) 下面是我的CellForRowatineXpath代码,如果有人能告诉我我做错了什么 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- (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配置CellWithImage方法:
- (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
}
}
}
错误应出现在函数configureCellWithImage和configureCellWithMessage上
我没有看到这些函数的代码,但我敢打赌您没有清除configureCellWithMessage上的图像内容。单元格得到重用。因此,每次都必须设置/重置单元格的所有属性 对于每个设置单元格属性的
if
语句,必须有一个重置相同属性的else
语句,即使它只是清除值
此外,每次使用单元时,必须避免反复添加子视图。您拥有创建图像视图并将其添加到单元的代码。但是您不断地添加新的图像视图。如果需要,只需添加一次即可。如果它已经存在,请使用新映像更新它,而不是添加新映像。您需要显示两个
configureCellWithXXX:
方法。ImageCellIdentifier和ChatMEssageCellIdentifier是两个单独的XIB吗?我添加了configureCellWithImage方法供您查看。谢谢我添加了configureCellWithImage方法供您查看。谢谢如果你也能用消息显示configureWithMessage,那就太棒了。这是给我的——你提到的两件事。