UICollectionVIew中的UIImageView在单击“单元格”之前不显示图像

时间:2015-05-05 22:57:20

标签: ios objective-c uiimageview uicollectionview uicollectionviewcell

在我的collectionView中,每个单元格都有一个图像。使用从mySQL数据库获取的URL从Web中提取图像。我的UICollectionView的整个代码都在这里,单元格是定制的:

#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
}

我的自定义UICollectionView单元格的代码几乎完全是空白的,除了XIB。将出现单元格,所有标签设置正确,等等。但是,imageView的图像显示为黑色(背景颜色),直到我点击它,在这种情况下,单元格的imageView(并且只有该单元格的imageView)有它的图像出现。我想知道如何在视图加载时显示这些图像,而不必点击它们。

0 个答案:

没有答案