0

我想在我的應用程序中掛接UICollectionView,並在每次加載它時都會看到一個黑屏。下面是我寫的代碼:UICollectionView無法通過編程方式加載

來電代碼:

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 
    [flowLayout setItemSize:CGSizeMake(200, 200)]; 
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 
    MyAppDashBoardHomeViewController *aDashboardViewController = [[MyAppDashBoardHomeViewController alloc] initWithCollectionViewLayout:flowLayout]; 
    [self presentViewController:aDashboardViewController animated:YES completion:nil]; 

UICollectionController子類

#import <UIKit/UIKit.h> 

@interface MyAppDashBoardHomeViewController : UICollectionViewController <UICollectionViewDelegate, UICollectionViewDataSource> 

@end 


#import "MyAppDashBoardHomeViewController.h" 
#import "MyAppDashBoardCollectionViewCell.h" 
#import "MyAppCustomNavigationBar.h" 

@interface MyAppDashBoardHomeViewController() 

@property (nonatomic, strong) NSMutableArray *applications; 
@property (nonatomic) UIView *containerView; 
@property (nonatomic, strong) MyAppCustomNavigationBar *customNavBar; 

@end 

@implementation MyAppDashBoardHomeViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view 

    [self.collectionView registerClass:[MyAppDashBoardCollectionViewCell class] forCellWithReuseIdentifier:@"MyAppDashBoardCell"]; 

    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height)]; 
    [self.containerView setBackgroundColor:[UIColor blackColor]]; 
    [self.containerView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]]; 
    [self.view addSubview:self.containerView]; 

    // Adding custom navigation bar 
    self.customNavBar = [[MyAppCustomNavigationBar alloc] initWithTitle:kMyAppNewTestTitle detailedTitle:nil informationBarData:nil buttonType:MyAppModalScreen andDelegate:self]; 
    [self.containerView addSubview:self.customNavBar]; 


    self.applications = [[NSMutableArray alloc] initWithObjects:@"Test 1", @"Test 2", @"Test 3", @"Test 4", nil]; 
} 



- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (NSInteger)collectionView:(UICollectionView *)iCollectionView numberOfItemsInSection:(NSInteger)iSection { 
    return self.applications.count; 
} 

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: 
- (UICollectionViewCell *)collectionView:(UICollectionView *)iCollectionView cellForItemAtIndexPath:(NSIndexPath *)iIndexPath { 
    static NSString *cellIdentifier = @"MyAppDashBoardCell"; 

    MyAppDashBoardCollectionViewCell *aCell = (MyAppDashBoardCollectionViewCell *)[iCollectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:iIndexPath]; 

    if (!aCell) { 
     aCell = [[MyAppDashBoardCollectionViewCell alloc] init]; 
    } 

    aCell.appName = self.applications[iIndexPath.row]; 

    return aCell; 
} 


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)iCollectionView { 
    return 1; 
} 


- (void)backButtonAction:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

UICollectionViewCell子類

#import <UIKit/UIKit.h> 

@interface MyAppDashBoardCollectionViewCell : UICollectionViewCell 

@property (nonatomic, strong) NSString *appName; 

@end 



#import "MyAppDashBoardCollectionViewCell.h" 

@interface MyAppDashBoardCollectionViewCell() 

@property (nonatomic, strong) UILabel *appNameLabel; 

@end 

@implementation MyAppDashBoardCollectionViewCell 

- (id)init { 
    self = [super init]; 

    if (self) { 
     self.appNameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     self.appNameLabel.textAlignment = NSTextAlignmentCenter; 
     self.appNameLabel.textColor = [UIColor whiteColor]; 
     self.appNameLabel.font = [UIFont systemFontOfSize:17.0]; 

     self.backgroundColor = [UIColor redColor]; 
     [self.viewForBaselineLayout addSubview:self.appNameLabel]; 
    } 

    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    CGSize aTextSize = [self.appName sizeWithFont:[UIFont systemFontOfSize:17.0]]; 
    CGRect appNameFrame = CGRectMake((self.frame.size.width - aTextSize.width)/2, (self.frame.size.height - aTextSize.height)/2, aTextSize.width, aTextSize.height); 
    self.appNameLabel.frame = appNameFrame; 
} 


@end 
+0

順便說一句,當你註冊一個類或筆尖,或者在故事板中創建單元格時,不需要檢查零單元格。你使用的出隊方法保證返回一個單元格(或者如果你沒有給出正確的標識符就會崩潰)。 – rdelmar 2014-10-07 23:19:06

回答

1

UICollectionViewCell的init方法應該是initWithFrame :(從UIView繼承),所以我認爲你的問題是你的普通init方法沒有被調用。

+0

確實。感謝您指出。 – Abhinav 2014-10-07 23:38:11

相關問題