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

SDWebImage and UITableViewCell with multiple UiImageViews doesn't show all images

我需要在每个 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 都不会更新,即使单元格仍然可见?

您在每次循环迭代中覆盖您的 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
    }];
}

试试这个。为我工作。

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

                }];
    }