Ios UICollectionViewController黑屏
我试图显示我的收藏视图,但我得到的只是一个黑屏。我知道我遗漏了什么,但我似乎找不到它是什么Ios UICollectionViewController黑屏,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我试图显示我的收藏视图,但我得到的只是一个黑屏。我知道我遗漏了什么,但我似乎找不到它是什么 #import "StoreCollectionViewController.h" #import "StoreItemCell.h" @interface StoreCollectionViewController () @property (nonatomic, strong) NSMutableArray *productsArray; @property (nonatomic, strong)
#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
#导入“StoreCollectionViewController.h”
#导入“StoreItemCell.h”
@接口StoreCollectionViewController()
@属性(非原子,强)NSMutableArray*productsArray;
@属性(非原子,强)UIActivityIndicatorView*加载指示器;
@结束
@实现StoreCollectionViewController
静态NSString*const reuseIdentifier=@“Cell”;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我)
{
self.productsArray=[[NSMutableArray alloc]init];
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
//注册单元类
[self.collectionView注册表类:[StoreItemCell类]forCellWithReuseIdentifier:reuseIdentifier];
CGFloat width=CGRectGetWidth(self.view.bounds);
CGFloat height=CGRectGetHeight(self.view.bounds);
self.loadingIndicator=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(宽度/2,高度/2,37,37)];
self.loadingIndicator.center=CGPointMake(宽/2,高/2-37);
self.loadingIndicator.autoresizingMask=(UIViewAutoresizingFlexibleRightMargin | uiviewautoresizingflexibleleleftmargin | uiviewautoresizingflexiblebtotommargin | uiviewautoresizingflexibleptopmargin | uiviewautoresizingflexibleptommargin;
self.loadingIndicator.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhiteLarge;
self.loadingIndicator.hidesWhenStopped=是;
[self.view addSubview:self.loadingIndicator];
[自加载指示器启动激活];
[自行发送];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)派遣{
NSURL*url=[NSURL URLWithString:@”http://api.bigcartel.com/littleheart/products.json"];
调度异步(调度获取全局队列(调度队列优先级默认为0)^{
NSData*data=[NSData dataWithContentsOfURL:url];
dispatch\u async(dispatch\u get\u main\u queue()^{
如果(数据==nil){
NSLog(“数据为零”);
UIAlertView*nilDataAlert=[[UIAlertView alloc]initWithTitle:@”“消息:@“这里什么都没有!”委托:自取消按钮:@“确定”其他按钮:nil,nil];
[nilDataAlert show];
[自加载指示器停止设置动画];
}
否则{
[self-performSelectorOnMainThread:@selector(getProducts:)withObject:data waitUntilDone:YES];
}
});
});
}
-(无效)getProducts:(NSData*)responseData{
n错误*错误;
NSMutableArray*responseArray=[NSJSONSerialization
JSONObjectWithData:responseData
选项:针织品
错误:&错误];
对于(NSDictionary*productDictionary in responseArray){
[self.productsArray addObject:productDictionary];
}
[self.collectionView-reloadData];
[自加载指示器停止设置动画];
}
#布拉格标记
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView{
返回1;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面{
返回self.productsArray.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
StoreItemCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
//配置单元格
NSString*productName=[[self.productsArray objectAtIndex:indexath.row]objectForKey:@“name”];
cell.itemNameLabel.text=productName;
返回单元;
}
#布拉格标记
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{
}
@结束
问题不在于你是否在收藏中加载了一些东西,而在于更一般的东西。这就是collectionViewController是否实例化以及是否正确加载。
所有根视图控制器视图都具有默认的白色。但是UIWindow类具有默认的黑色。如果您看到黑色,我怀疑窗口是空的,您是否使用了UICollectionViewDelegateFlowLayout方法来调整单元格大小 像这样(这是在Swift中,使用Objective-C syntex)
确保productsArray不为空或为零。如果是,则不会生成任何单元格。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake(50,120); //use whatever you wants.
}