mardi 5 mai 2015

UIImageView in UICollectionVIew does not display image until "cell" is clicked on

In my collectionView, each cell has an image. The image is pulled from the web with a URL obtained from mySQL database. The entire code of my UICollectionView is here, and the cells are custom made:

#import "DealsCollectionView.h"
#import "DealsCell.h"
#import "DealsModel.h"
#import "Deal.h"

@interface DealsCollectionView ()
{
DealsModel *_homeModel;
}

@property UIActivityIndicatorView *spinner;
@property NSMutableArray *deals;
@property NSCache *imageCache;

@end

@implementation DealsCollectionView

-(void)itemsDownloaded:(NSArray *)items
{
// This delegate method will get called when the items are finished downloading

// Set the downloaded items to the array
_deals = [items copy];

[_spinner stopAnimating];

// Reload the table view
[self.collectionView reloadData];
}


- (instancetype) initWithCollectionViewLayout:(UICollectionViewLayout *)layout
{
self = [super initWithCollectionViewLayout:layout];

if (self) {
    // alloc and init the various (Mutable)Array properties
    self.deals = [[NSMutableArray alloc] init];
    self.imageCache = [[NSCache alloc] init];

    // Set Title
    self.navigationItem.title = @"Deals from Our Sponsors";



    // Create new HomeModel object and assign it to _homeModel variable
    _homeModel = [[DealsModel alloc] init];

    // Set this view controller object as the delegate for the home model object
    _homeModel.delegate = self;

    // Call the download items method of the home model object
    [_homeModel downloadItems];

}

return self;
}


- (void)viewDidLoad {
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;

// Create a spinner
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
                                    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

spinner.center = CGPointMake(screenWidth/2.0, screenHeight/5.0);
spinner.hidesWhenStopped = YES;
[self.view addSubview:spinner];
[spinner startAnimating];
_spinner = spinner;



// Do any additional setup after loading the view.

UINib *cellNib = [UINib nibWithNibName:@"DealsCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"dealCell"];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _deals.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DealsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dealCell" forIndexPath:indexPath];

// Configure the cell
Deal *thisDeal = [_deals objectAtIndex:indexPath.row];

UILabel *nameLabel = (UILabel *) [cell viewWithTag:1];
nameLabel.text = thisDeal.name;

UILabel *offerLabel = (UILabel *) [cell viewWithTag:2];
offerLabel.text = thisDeal.offer;

UILabel *descriptionLabel = (UILabel *) [cell viewWithTag:3];
descriptionLabel.text = thisDeal.businessDescription;

UILabel *addressLabel = (UILabel *) [cell viewWithTag:4];
addressLabel.text = thisDeal.address;



// Configure the image
UIImageView *imageView = (UIImageView *) [cell viewWithTag:6];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;

UIImage *dealLargeImage = [self.imageCache objectForKey:thisDeal.largeImage];
imageView.image = dealLargeImage;
if (dealLargeImage == nil) {

    NSURLSessionConfiguration *sessionConfig =
    [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate: self delegateQueue:nil];
    NSURLSessionDataTask *imageData = [session dataTaskWithURL:[NSURL URLWithString: thisDeal.largeImage]
                                             completionHandler:^(NSData *data,
                                                                 NSURLResponse *response,
                                                                 NSError *error) {
                                                 // handle NSData
                                                 UIImage *image = [UIImage imageWithData:data];
                                                 //thisDeal.image = image;
                                                 [self.imageCache setObject:image forKey:thisDeal.largeImage];
                                                 imageView.image = image;

                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                     [self.collectionView reloadData];
                                                 });
                                             }];
    [imageData resume];
}

return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20, 10, 20, 10); // top, left, bottom, right
}

The code for my custom UICollectionView cells are pretty much entirely blank, except for the XIB. The cell will appear, with all the labels set appropriately, etc. However, the image of the imageView is shown as black (the background color), until I click on it, in which case that cell's imageView (and only that cell's imageView) has its image appear. I'm wondering how I can get these images to appear as the view loads, rather than having to click on them.

Aucun commentaire:

Enregistrer un commentaire