-
開始考慮好一點點時間,因為一般的都是用xib,或者storyboard來寫的.這次用純代碼...廢話較多請看
-
首先把storyboard干掉,工程里面的main干掉
-
由於干掉了storyboard則啟動的控制器要在Appdelegate中指定
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UICollectionViewFlowLayout * fly = [UICollectionViewFlowLayout new];
fly.itemSize = CGSizeMake(50, 50);
fly.sectionInset = UIEdgeInsetsMake(50, 10, 0, 10);
//創建collectionViewController並設置布局參數
ViewController * vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
UICollectionView * colView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:fly];
vc.collectionView = colView;
//必須注冊可重用id
[vc.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[vc.collectionView setCollectionViewLayout:fly];
vc.collectionView.backgroundColor = [UIColor blueColor];
[self.window makeKeyAndVisible];
return YES;
}
- 上述代碼中,一定要在創建viewcontroller的時候給它的collectionView指定布局參數,否則一直會報錯,報錯原因就是:"請給UICollectionView初始化一個non-nil的layout(布局參數)",如果報這個錯誤而你在控制的.m文件中viewDidload中給self.CollectionView 設置並且初始化了一個布局參數,再次運行程序依然會報上面的錯誤.
- 所以創建控制器的時候給控制器.collectionView指定布局參數.
- 指定布局參數后還會報錯,說:請給cell設置一個可重用ID,當你在viewdidload中注冊了一個Id后依然報錯,打全局斷點會崩到數據源第三個方法中...此時正確的解決方式就是,在Appdelegate中創建控制器的時候讓控制器的.collectionView register 注冊一個可重用id.此時再運行正確.
- 注意點1: 此時打斷點在Viewcontroller中的viewdidload方法,----結果是根本不會執行viewdidload,所以它才報錯讓你注冊一個可重用id,如果在Appdelegate中的控制器的.collectionView注冊一個可重用ID則 解決.
#import "ViewController.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
/** 布局參數 */
//@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@end
@implementation ViewController
//此時懶加載沒有用 因為viewdidload中的方法根本不會執行
//-(UICollectionViewFlowLayout *)flowLayout{
//
// if (_flowLayout == nil) {
// _flowLayout = [[UICollectionViewFlowLayout alloc] init];
// }
// return _flowLayout;
//}
//不執行---
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor = [UIColor whiteColor];
// self.collectionView.dataSource = self;
// self.collectionView.delegate = self;
// [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"dianji");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString * const reuseIdentifier = @"Cell";
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Snip20160422_5"]];
return cell;
}
@end
綜上: 純 代碼創建collectionView,以及collectionVIewController,在哪里創建的collectionViewcontroller就在哪里設置布局參數以及cell的可重用ID即可. 切記切記...demo地址:http://pan.baidu.com/s/1i4KLlbv