I am trying to show my viewing of a collection, but all I get is a black screen. I know that something is missing, but I can not find what it is.
#import "StoreCollectionViewController.h"
#import "StoreItemCell.h"
@interface StoreCollectionViewController ()
@property (nonatomic, strong) NSMutableArray *productsArray;
@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;
@end
@implementation StoreCollectionViewController
static NSString * const reuseIdentifier = @"Cell";
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.productsArray = [[NSMutableArray alloc]init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[StoreItemCell class] forCellWithReuseIdentifier:reuseIdentifier];
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
self.loadingIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(width / 2, height / 2, 37, 37)];
self.loadingIndicator.center = CGPointMake(width / 2, height / 2 - 37);
self.loadingIndicator.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin);
self.loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
self.loadingIndicator.hidesWhenStopped = YES;
[self.view addSubview:self.loadingIndicator];
[self.loadingIndicator startAnimating];
[self dispatch];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dispatch {
NSURL *url = [NSURL URLWithString:@"http://api.bigcartel.com/littleheart/products.json"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
if (data == nil) {
NSLog(@"data is nil");
UIAlertView *nilDataAlert = [[UIAlertView alloc]initWithTitle:@"" message:@"There is nothing here!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[nilDataAlert show];
[self.loadingIndicator stopAnimating];
}
else {
[self performSelectorOnMainThread:@selector(getProducts:) withObject:data waitUntilDone:YES];
}
});
});
}
- (void)getProducts:(NSData *)responseData {
NSError *error;
NSMutableArray *responseArray = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
for (NSDictionary *productDictionary in responseArray) {
[self.productsArray addObject:productDictionary];
}
[self.collectionView reloadData];
[self.loadingIndicator stopAnimating];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.productsArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
StoreItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSString *productName = [[self.productsArray objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.itemNameLabel.text = productName;
return cell;
}
#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
}
@end
source
share