ios - UICollectionView 未以编程方式加载

标签 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

最佳答案

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

关于ios - UICollectionView 未以编程方式加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26246723/

相关文章:

ios - 在 iOS 中正确设置 UILabel 的背景图片

iphone - Objective-C : How to make a subclass of UIControl react like a button

objective-c - 尝试访问托管对象属性时的 EXC_BAD_ACCESS

ios - 为什么 SKProductsRequest 需要产品标识符?

ios - MYSQL 和 Swift - 上传图像和文件 ||使用 Alamofire 会更好吗?

objective-c - 如何在 gdb 中打印 objective-c 类方法的结果?

objective-c - 在 -viewWillLayoutSubviews 中调用的方法莫名其妙地运行了两次

ios - 如何获取 UIButton 在 Tableview 单元格中的位置,并在 Swift 中添加目标?

ios - 替换了viewController

iPhone 如何设置帧速率并减慢 AVCapture didOutputSampleBuffer Delegate