gpt4 book ai didi

ios - UICollectionViewController 黑屏

转载 作者:可可西里 更新时间:2023-11-01 05:43:58 28 4
gpt4 key购买 nike

我试图显示我的收藏 View ,但我得到的只是黑屏。我知道我在某处遗漏了一些东西,但我似乎找不到它是什么。

#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

最佳答案

您是否使用过UICollectionViewDelegateFlowLayout 方法来调整单元格大小?

像这样(这是在 Swift 中,使用 Objective - C 语法),

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake(50,120); //use whatever you wants.
}

关于ios - UICollectionViewController 黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28842296/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com