在tableViewCell中动态更改UIImageView的宽度和高度

时间:2013-07-17 18:51:55

标签: ios uitableview uiimageview xib

我有自定义的UITableViewCell,它是由xib文件制作的。 我在该单元格内部有UIImageView,我设置了固定的宽度和高度以及40x40并设置为隐藏

然后在cellForRowAtIndexPath数据源委托中。我检查该单元格的数据源是否包含图像的URL。如果有,我将ImageView设置为出现。并在下载完成时使用SDWebImage下载图像并在完成块中调整imageView.frame的大小

实际上,在第一次出现它工作正常。我得到了相同大小的ImageView和下载的图像。但是,如果我向下滚动然后再向上移动到上部单元格。它重新出现40x40尺寸增益。有人可以帮忙吗?

这是代码,以防您好奇。我觉得代码没问题,问题是如何在.xib文件中设置UIImageView,但我不知道如何解决它。

//    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

if([item postImageURLString]){

        void (^completionBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType) = ^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

            CGRect imageFrame = CGRectMake(cell.frame.size.width/3,cell.frame.size.height -5 -image.size.height, image.size.width, image.size.height);

            [cell.postImageView setFrame:imageFrame];


        };

            [cell.postImageView setHidden:NO];


        [cell.postImageView setImageWithURL:URL completed:completionBlock ];

    }

1 个答案:

答案 0 :(得分:0)

我怀疑它与tableview重用具有40x40图像视图的单元格有关。它看起来像SDWebImage will not run your completion block if the image is already cached,因此如果tableview重绘单元格并且您的图像已经被缓存,则它永远不会执行调整大小代码。您需要向cellForRowAtIndexPath方法添加代码,该方法检查图像是否已经缓存,如果是,则调整图像视图的大小。