1

I have a custom cell with UIImageView, and it is not showing the image. I have tried setting image to default cell.imageView.image property, and it works just fine, but doesn't work with my custom ImageView.

I load my custom Cell from Xib, and I believe it has to do with lazy loading of UIImageView. How do I make it work? Here is my code:

- (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;
}
4

1 回答 1

4

I usually do Lazy loading for ImageViews with this piece of code, hope it helps:

- (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;
            }
        });

    }];
}

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

Where imageCell is your UIImageView.

于 2013-03-30T14:27:19.247 回答