UITableViewCell不显示从服务器下载的图像

时间:2014-07-21 15:38:11

标签: ios objective-c uitableview uiimageview uiimage

我的表格是“搜索栏和搜索显示控制器”的副产品,其中UITableViewCell是由xib定制的。下面是Cell的代码。我无法发现为什么图像没有显示。有人请帮我解决问题吗?显示所有其他数据,但imageView的内容除外。

.h

#import <UIKit/UIKit.h>

@interface BCDDogSearchTableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *dogImageView;
@property (strong, nonatomic) IBOutlet UITextView *dogDescriptionView;
@property(strong,nonatomic) NSString *imageURL;


@end

.m

#import "BCDDogTableViewCell.h"
#import "FICImageCache.h"

@interface BCDDogTableViewCell()

@property(strong,nonatomic) UIImage *image;

@end

@implementation BCDDogTableViewCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setImageURL:(NSString *)imageURL
{
    _imageURL=imageURL;
    [self startDownloadingImage];
}

-(void)startDownloadingImage
{
    self.image=nil;
    if (self.imageURL) {
        NSLog(@"The imageURL is %@",self.imageURL);

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.imageURL]];
        NSURLSessionConfiguration *config=[NSURLSessionConfiguration ephemeralSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
        NSURLSessionDownloadTask *task =
            [session downloadTaskWithRequest:request
                           completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
                               if (!error) {
                                   if ([request.URL isEqual:self.imageURL]) {//check in case things have changed somehow
                                       UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:localfile]];
                                       dispatch_async(dispatch_get_main_queue(), ^{self.image=image;});//ui thread.
                                   }
                               }else NSLog(@"loading image error: %@",error);
                           }];
        [task resume];
    }

}

-(void)setImage:(UIImage *)image
{
    NSLog(@"set image to the image view");
    self.dogImageView.image=image;
}

-(UIImage *)image
{
     NSLog(@"get image from imageView");
    return self.dogImageView.image;
}

-(UIImageView *) dogImageView
{
    if (!_dogImageView) _dogImageView=[[UIImageView alloc]init];
    return _dogImageView;
}


@end

更新

我在以下代码段中添加了更多日志记录

if (!error) {
  NSLog(@"Completion block before if");
  if ([request.URL isEqual:self.imageURL]) {//check in case things have changed somehow
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:localfile]];
    dispatch_async(dispatch_get_main_queue(), ^{self.image=image;});//ui thread.
  }else{
    NSLog(@"request url %@",request.URL);
    NSLog(@"self url %@", self.imageURL);
  }
}else NSLog(@"loading image error: %@",error);

根据日志记录,if ([request.URL isEqual:self.imageURL])保持返回false,尽管在else子句中打印的两个url的内容相同。

所以我删除了,如果检查并突然显示图像。但我担心它只能起作用,因为我的桌子上有一排。那么有人知道为什么检查失败了,显然它是同一个网址吗?

1 个答案:

答案 0 :(得分:0)

我发现问题,在我的if语句中我将一个url与一个字符串进行比较。

if ([request.URL isEqual:self.imageURL])

感谢大家的帮助。