具有多个 UiImageViews 的 SDWebImage 和 UITableViewCell 不显示所有图像

Posted

技术标签:

【中文标题】具有多个 UiImageViews 的 SDWebImage 和 UITableViewCell 不显示所有图像【英文标题】:SDWebImage and UITableViewCell with multiple UiImageViews doesn't show all images 【发布时间】:2016-04-21 11:53:29 【问题描述】:

我需要在每个 UITableViewCell 中显示多个图像。为此,我使用 SDWebImage 异步下载图像。我在 UITableViewCell 的 configCell 方法中运行以下代码:

        for (int i=0; i<allOrganizationIds.count; i++) 
            self.orgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - 10) - (55 * position), 3, 50, 15)];
            org = [[DLOrganizationManager getInstance] organizationForId:[allOrganizationIds[i] intValue]];

            [self.orgView sd_setImageWithURL:[NSURL URLWithString:org.organizationLogoUrl] placeholderImage:[UIImage imageNamed:@"category-selected"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) 
                [self addSubview:self.orgView];
            ];
        

问题是每个单元格只显示一个图像,即使应该有三个。 complete 块只执行一次。当将特定单元格滚动出视图并返回时,所有图像都是可见的。

为什么每次成功下载图像时 UIImageView 都不会更新,即使单元格仍然可见?

【问题讨论】:

【参考方案1】:

您在每次循环迭代时覆盖您的 orgView 属性,这意味着您创建的第一个视图在第二次迭代后不久就会被释放,因为它没有被任何人保留。

此外,您添加的每个图像视图都具有相同的帧,因为位置变量在 for 循环的范围内不会改变。您可能应该在框架计算中使用您的 i 变量。

for (int i=0; i<allOrganizationIds.count; i++) 
    UIImageView *orgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - 10) - (55 * position), 3, 50, 15)];
    [self addSubview:orgView]; // The image view is then strongly retained by it's superview
    org = [[DLOrganizationManager getInstance] organizationForId:[allOrganizationIds[i] intValue]];

    [orgView sd_setImageWithURL:[NSURL URLWithString:org.organizationLogoUrl] placeholderImage:[UIImage imageNamed:@"category-selected"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) 
        // Do wathever you want here
        // If this view is opaque without an image in it you can play with the hidden property to acheive the same effect as if you added it as a subview here
    ];

【讨论】:

您好 Antoine,感谢您的快速回复。确实,我使用了一种我不断覆盖的视图。但正如我所指出的,起初,只有一个图像是可见的。当将单元格滚动出视图并立即返回时,所有图像都在那里。所以这似乎有效。每次迭代中的帧都不相同。 “位置”变量一直在变化,并在 x 轴上移动图像。 @oliver 您是否尝试将 addSubview 调用从完成块移动到 for 循环? 另外,因为你没有在循环中修改你的位置变量并且你没有保留对你的视图的任何引用,所以你的子视图不可能有不同的框架。当您将单元格从屏幕上移开并返回时,您的单元格会被表格视图入队和出队,并且您可能会再次调用此方法(从方法 cellForRowAtIndexPath)使用不同的位置值,这就是您看到图像出现的原因当你滚动时。 安托万,感谢您的想法。我根据您的建议更改了我的代码(在每次迭代中添加一个 UIImageView + 将其添加为已完成块之外的子视图),它终于可以工作了。 我担心单元格不会释放 UIImageView,因此会在每个其他出列单元格上显示所有添加的图像。但出乎我的意料,它似乎确实如此。【参考方案2】:

试试这个。为我工作。

    for (int i=0; i<allOrganizationIds.count; i++) 
    
                self.orgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - 10) - (55 * position), 3, 50, 15)];

                org = [[DLOrganizationManager getInstance] organizationForId:[allOrganizationIds[i] intValue]];

                NSURL *ImgURL = [NSURL URLWithString:[org.organizationLogoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];    

                [self.orgView sd_setImageWithURL:ImgURL placeholderImage:[UIImage imageNamed:@"category-selected"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) 
                


                    [self addSubview:self.orgView];

                ];
    

【讨论】:

感谢您的回复。您的方法加快了图像加载过程。但是细胞的行为没有改变。

以上是关于具有多个 UiImageViews 的 SDWebImage 和 UITableViewCell 不显示所有图像的主要内容,如果未能解决你的问题,请参考以下文章

比较具有 UIImageViews 的 UIButtons

iOS项目相关@AFN&SDWeb的二次封装

如何使用 IBOutletCollection 将多个 UIImageViews 连接到同一个插座?

将多个 UIImageViews 添加到子类 UIView

如何将多个 UIImageViews 添加到分页 UIScrollView?

使 UIImagePickerController 将所选图像设置为多个 UIImageViews