【问题标题】:UITableView imageView reuse other cell's imageUITableView imageView 复用其他单元格的图片
【发布时间】:2016-05-04 04:43:52
【问题描述】:

我需要创建一个UITableView,并在其中发布不同数量的图像,例如FacebookTwitterWeChat 朋友。返回动态单元格高度并根据它们的数量排列图像。

它应该看起来像这样

我将图像的动态单元格高度设置为:

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

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

Cell 确实有图片效果很好,它取决于包含不同数量的图片 URL 字符串的 imagesArray

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

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

}

所以最重要的是,重用 imageViewCollection 的问题,有什么建议吗?对此表示赞赏。

【问题讨论】:

  • 您应该先将 imageview 设置为 nil,然后再尝试重用单元格。基本上在您尝试使用 sd_setImageWithURL 下载图像之前放置它

标签: ios uitableview uiimageview


【解决方案1】:

您必须使用两个不同的单元格reuseIdentifier。一个标识符(例如 cell1)用于具有图像的单元格(例如 cell2)和一个用于不具有图像的单元格。所以你要取两个不同的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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多