UITableView imageView重用其他单元格的图像

时间:2016-05-04 04:43:53

标签: ios uitableview uiimageview

我需要在UITableView中张贴不同数量的图片,例如FacebookTwitterWeChat朋友。返回动态单元格高度并排列图像取决于它们的数量。

它看起来应该是sample image 1

我为图像执行动态单元格高度:

HomeTableViewCell.m

中的

将包含帖子图像的视图的高度自动布局约束设为属性

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height;

并将其常量设置为不同的值以使单元格动态高度

self.height.constant = imageArray.count > 3 ? 260.0 : imageArray.count == 0 ? 0.0 : 130.0;

并且还将文字的高度设为:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;

}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    return 130 + cell.height.constant;
}

完成后,它与第一张图片的效果相同,然后我通过SDWebImage框架加载图像,我在单元格中创建最多6张图像的imageCollection

@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imageViewCollection;

- (void)loadCellDataWithInfo: (SharedUserInfo *)info {

self.userInfo = info;
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:info.profileImageUrlStr] placeholderImage:[UIImage imageNamed:@"penguinMe"]];

self.avatarImageView.layer.cornerRadius = self.avatarImageView.frame.size.width/2.0;
self.name.text = info.fullName;
self.username.text = info.userName;
self.text.text = [info.postDic objectForKey:@"text"];
self.likeLabel.text = [NSString stringWithFormat:@"%li", [[info.postDic objectForKey:@"likesCount"]integerValue]];
self.commentLabel.text = [NSString stringWithFormat:@"%li", [[info.postDic objectForKey:@"commentsCount"]integerValue]];
self.elapsed.text = [info.postDic objectForKey:@"elapsed"];

NSArray *imageArray = [info.postDic objectForKey:@"images"];

[self loadImagesWithURLStr:imageArray];

self.height.constant = imageArray.count > 3 ? 260.0 : imageArray.count == 0 ? 0.0 : 130.0;

}

- (void)loadImagesWithURLStr: (NSArray *)urlStrArray {

switch (urlStrArray.count) {
    case 0:
        break;
    case 4:
        for (int index = 0; index < urlStrArray.count; index++) {
            if (index >= 2) {
                [self.imageViewCollection[index + 1] sd_setImageWithURL:[NSURL URLWithString:urlStrArray[index]]];
            } else {
                [self.imageViewCollection[index] sd_setImageWithURL:[NSURL URLWithString:urlStrArray[index]]];
            }
        }
        break;

    default:
        for (int index = 0; index < urlStrArray.count; index++) {
            [self.imageViewCollection[index] sd_setImageWithURL:[NSURL URLWithString:urlStrArray[index]]];
        }
        break;
}  
}

现在我遇到了问题,有些没有图像的单元格会反复从其他地方加载一些图像,似乎只有没有任何帖子图像的单元格才会遇到这样的问题

post sample 2

enter image description here

Cell确实有图像效果很好,它取决于imagesArray包含不同数量的图像URL String

最后我的所有重用代码和单元部分代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

[cell loadCellDataWithInfo:self.followingArray[indexPath.row]];

return cell;
}

- (void)loadCellDataWithInfo: (SharedUserInfo *)info {

self.userInfo = info;
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:info.profileImageUrlStr] placeholderImage:[UIImage imageNamed:@"penguinMe"]];

self.avatarImageView.layer.cornerRadius = self.avatarImageView.frame.size.width/2.0;
self.name.text = info.fullName;
self.username.text = info.userName;
self.text.text = [info.postDic objectForKey:@"text"];
self.likeLabel.text = [NSString stringWithFormat:@"%li", [[info.postDic objectForKey:@"likesCount"]integerValue]];
self.commentLabel.text = [NSString stringWithFormat:@"%li", [[info.postDic objectForKey:@"commentsCount"]integerValue]];
self.elapsed.text = [info.postDic objectForKey:@"elapsed"];

NSArray *imageArray = [info.postDic objectForKey:@"images"];

[self loadImagesWithURLStr:imageArray];

self.height.constant = imageArray.count > 3 ? 260.0 : imageArray.count == 0 ? 0.0 : 130.0;

}

enter image description here

首先,这是重用imageViewCollection的问题,有什么建议吗?赞赏。

1 个答案:

答案 0 :(得分:0)

您必须使用两个不同的单元格reuseIdentifier。具有图像的单元(例如,单元格2)和单元格的一个标识符(例如,单元格1)不具有图像。所以你必须采用两个不同的UITableViewCell

代码如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(imagesArr.count == 0)
{
cellIdentifier = @"cell1";
}
else{
cellIdentifier = @"cell2";
}
 HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


 [cell loadCellDataWithInfo:self.followingArray[indexPath.row]];

  return cell;
}