#import "StoreCollectionVIEwController.h"#import "StoreItemCell.h"@interface StoreCollectionVIEwController ()@property (nonatomic,strong) NSMutableArray *productsArray;@property (nonatomic,strong) UIActivityIndicatorVIEw *loadingIndicator;@end@implementation StoreCollectionVIEwControllerstatic 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 cancel@R_301_5554@Title:@"OK" other@R_301_5554@Titles: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 syntex),
func collectionVIEw(collectionVIEw: UICollectionVIEw,layout collectionVIEwLayout: UICollectionVIEwLayout,sizeforItemAtIndexPath indexPath: NSIndexPath) -> CGSize{ return CGSizeMake(50,120); //use whatever you wants.}总结
以上是内存溢出为你收集整理的ios – UICollectionViewController黑屏全部内容,希望文章能够帮你解决ios – UICollectionViewController黑屏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)