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

时间:2016-04-21 11:53:30

标签: ios objective-c uitableview uiimageview sdwebimage

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

试试这个。为我工作。

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

                }];
    }