Ios UIImageView不显示图像的自定义UITableViewCell

Ios UIImageView不显示图像的自定义UITableViewCell,ios,objective-c,ios5,ios6,Ios,Objective C,Ios5,Ios6,我有一个带有UIImageView的自定义单元格,它没有显示图像。我已经尝试将image设置为默认的cell.imageView.image属性,它工作正常,但不适用于我的自定义imageView 我从Xib加载自定义单元格,我相信这与UIImageView的延迟加载有关。我如何让它工作? 这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)ind

我有一个带有UIImageView的自定义单元格,它没有显示图像。我已经尝试将image设置为默认的cell.imageView.image属性,它工作正常,但不适用于我的自定义imageView

我从Xib加载自定义单元格,我相信这与UIImageView的延迟加载有关。我如何让它工作? 这是我的密码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    DVGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];



    cell.tag = indexPath.row;

    if (self.loader.parsedData[indexPath.row] != nil)
    {
        cell.imageCustom.image = nil;

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
            dispatch_async(queue, ^(void) {

                NSString *url = [self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"];
                NSData *imageData = nil;
                if ([self.cache objectForKey:url] != nil)
                {
                    imageData = [self.cache objectForKey:url];
                }

                else
                {
                    imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
                [self.cache setObject:imageData forKey:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]];
                }
                dispatch_async(dispatch_get_main_queue(), ^{

                    if (cell.tag == indexPath.row) {
                        UIImage *image = [[UIImage alloc] initWithData:imageData];
                        cell.imageCustom.image = image;
                        [cell setNeedsLayout];
                    }
                });
            });
    }

    return cell;
}

我通常使用这段代码对ImageView进行延迟加载,希望能有所帮助:

- (void) loadImageForImageView:(UIImageView *)theImageView WithURL:(NSURL *)url {    

    NSOperationQueue *queue = [NSOperationQueue new];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:3.0];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *reponse, NSData *data, NSError *error) {

        UIImage *image = [UIImage imageWithData:data];

        dispatch_async(dispatch_get_main_queue(), ^{

            theImageView.image = image;
            for (UIActivityIndicatorView *spinner in theImageView.subviews) {
                [spinner removeFromSuperview];
                break;
            }
        });

    }];
}
在您的cellForRowAtIndexPath中:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner setColor:[UIColor darkGrayColor]];
spinner.frame = CGRectMake(130 , 53, 20, 20);
[spinner startAnimating];
[imageCell addSubview:spinner];
[self loadImageForImageView:imageCell WithURL:imageURL];

其中imageCell是您的UIImageView。

我通常使用这段代码对ImageView进行延迟加载,希望它能帮助您:

- (void) loadImageForImageView:(UIImageView *)theImageView WithURL:(NSURL *)url {    

    NSOperationQueue *queue = [NSOperationQueue new];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:3.0];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *reponse, NSData *data, NSError *error) {

        UIImage *image = [UIImage imageWithData:data];

        dispatch_async(dispatch_get_main_queue(), ^{

            theImageView.image = image;
            for (UIActivityIndicatorView *spinner in theImageView.subviews) {
                [spinner removeFromSuperview];
                break;
            }
        });

    }];
}
在您的cellForRowAtIndexPath中:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner setColor:[UIColor darkGrayColor]];
spinner.frame = CGRectMake(130 , 53, 20, 20);
[spinner startAnimating];
[imageCell addSubview:spinner];
[self loadImageForImageView:imageCell WithURL:imageURL];

其中imageCell是您的UIImageView。

如果在发布关于同一代码块的问题之间只有2小时的间隔,则您没有尽力自己解决问题。如果在发布关于同一代码块的问题之间只有2小时的间隔,则您没有尽力自己解决问题。