ios

  • 开始考虑好一点点时间,因为一般的都是用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

原文地址:https://www.cnblogs.com/adampei-bobo/p/5423242.html