我正在尝试显示我的收藏视图,但我得到的只是黑屏。我知道我在某个地方遗失了一些东西,但我似乎无法找到它是什么。
#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
答案 0 :(得分:2)
您是否使用 UICollectionViewDelegateFlowLayout 方法调整单元格大小。?
喜欢这个(这是在Swift中,使用Objective-C syntex ),
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake(50,120); //use whatever you wants.
}
答案 1 :(得分:1)
问题是我想不是你是否在集合中加载了一些东西,而是更通用的东西。这就是collectionViewController是否完全实例化以及是否正确加载。 所有根视图控制器视图都具有默认的白色。但UIWindow类具有默认的黑色。如果你看到黑色,我怀疑寡妇是空的,