UICollectionView无法以编程方式加载

时间:2014-10-07 23:07:51

标签: ios objective-c cocoa-touch uicollectionview uicollectionviewcell

我正在尝试在我的应用程序中连接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

1 个答案:

答案 0 :(得分:1)

UICollectionViewCell的init方法应该是initWithFrame :(继承自UIView),所以我认为你的问题是你的普通初始化方法没有被调用。