【发布时间】:2014-12-02 12:18:15
【问题描述】:
我正在尝试在我的应用程序中连接 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
【问题讨论】:
-
顺便说一句,当您注册课程或 nib 或在情节提要中制作单元格时,无需检查 nil 单元格。您使用的 dequeue 方法保证会返回一个单元格(如果您没有给出正确的标识符,则可能会崩溃)。
标签: ios objective-c cocoa-touch uicollectionview uicollectionviewcell