Reputation: 3600
I am trying to show my collection view but all I get is a black screen. I know I'm missing something somewhere but I can't seem to 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;
// Register cell classes
[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];
// Dispose of any resources that can be recreated.
}
- (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];
// Configure the cell
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
Upvotes: 4
Views: 3172
Reputation: 16820
Have you used UICollectionViewDelegateFlowLayout methods to size cell.?
Like this(This is in Swift, use Objective - C syntex),
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake(50,120); //use whatever you wants.
}
Upvotes: 2
Reputation: 7466
The question is I think not whether you load something into the collection, but something more general. That is whether the collectionViewController is instantiated at all and whether it is properly loaded. All root view controller views have a default white color. But the UIWindow class has a default black color. If you see black, I suspect the widow is empty,
Upvotes: 1