问题描述
- UIConllectionViewController
-
写个类继承 UICollectionViewController并设置成 rootViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
XuCollectionViewController *xu = [[XuCollectionViewController alloc]init];
self.window.rootViewController = xu;
return YES;
}这是 .h 文件
#import
@interface XuCollectionViewController : UICollectionViewController
@end
这是 .m 文件
- (void)viewDidLoad {
[super viewDidLoad];UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//cell间距
layout.minimumInteritemSpacing = 5.0f;
//cell行距
layout.minimumLineSpacing = 1.0f;
[layout setItemSize:CGSizeMake(30, 30)];self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
}
运行时 会崩溃 'UICollectionView must be initialized with a non-nil layout parameter'
一直没找到原因 ,哪位大神 给看下。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
解决方案
解决掉了 初始化方法 XuCollectionViewController *xu = [[XuCollectionViewController alloc]init]; 写错了 应该是 XuCollectionViewController *xu = [[XuCollectionViewController alloc]initWithCollectionViewLayout:layout];