ios - 纯代码创建collectionView
开始考虑好一点点时间,因为一般的都是用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
ios - 纯代码创建collectionView的更多相关文章
- iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距
之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...
- iOS开发 纯代码创建UICollectionView
转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html iOS开发 纯代码创建UICollectionView 习惯了使用xi ...
- iOS回顾笔记( 01 )-- XIB和纯代码创建应用的对比
header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...
- swift 之 纯代码创建 cell
初学swift 但是网上只有很多swift用xib创建的cell,就算是有也不是我想要的.今天自己弄了一个不用xib纯代码写的,来上代码 博客地址: https://github.com/liguol ...
- 【Android】纯代码创建页面布局(含异步加载图片)
开发环境:macOS 10.12 + Android Studio 2.2,MinSDK Android 5.1 先看看总体效果 本示例是基于Fragment进行的,直接上代码: [界面结构] 在 F ...
- iOS纯代码工程手动快速适配
首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...
- iOS纯代码手动适配 分类: ios技术 2015-05-04 17:14 239人阅读 评论(0) 收藏
首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...
- iOS Swift 开发语言之初接触,纯代码创建UIView,UITableView,UICollectionView
1. 初始化Label设置AttributeString override func viewDidLoad() { let label = UILabel(frame:CGRect(x:,y:,wi ...
- Object-C iOS纯代码布局 一堆代码可以放这里!
前言: 最近写的文章都是创业类,好吧,今天好好写写技术类的文章! 不过分享的不是IOS相关的文章,毕竟这几天在速成IOS,看的是object-c,由于速成的很快,好累! 好在现在基本已经入了点门道了, ...
随机推荐
- Motion images compression and restoration based on computer vision
This technique should apply to both normal video (consequtive sequences of pictures of real world) a ...
- topcoder SRM 618 DIV2 MovingRooksDiv2
一开始Y1,Y2两个参数看不懂,再看一遍题目后才知道,vector<int>索引代表是行数,值代表的是列 此题数据量不大,直接深度搜索即可 注意这里深度搜索的访问标识不是以前的索引和元素, ...
- 【BZOJ】1856: [Scoi2010]字符串
http://www.lydsy.com/JudgeOnline/problem.php?id=1856 题意:把n个1和m个0组成字符串,要求在组成的字符串中,任意的前k个字符1的个数不能少于0的个 ...
- BZOJ2322: [BeiJing2011]梦想封印
Description 渐渐地,Magic Land上的人们对那座岛屿上的各种现象有了深入的了解. 为了分析一种奇特的称为梦想封印(Fantasy Seal)的特技,需要引入如下的概念: 每一位魔法的 ...
- IOS本地通知
发送通知: UILocalNotification *newNotification = [[UILocalNotification alloc] init]; if (newNotifica ...
- flex lineChart 显示所有的数据节点
.If you're using <mx:LineSeries>, then set the following property:itemRenderer="mx.charts ...
- NHibernate 基本配置 (第一篇)
使用NHibernate最重要的一步就是配置,如果连NHibernate都还没有跑的起来,谈何学习.今天就来详解一下NHibernate的配置. 一.NHibernate基本配置 NHibernate ...
- 【android studio】解决android studio drawable新建项目时只有一个drawable目录的问题
概述 android studio默认新建Module时,只新建一个drawable目录,并不会新建适配不同分辨率的drawable目录.但其实,这是可以设置的.有以下两种方法: 方法1 详细步骤 进 ...
- jQuery的常用事件
1.$(document).ready() $(document).ready()是jQuery中响应JavaScript内置的onload事件并执行任务的一种典型方式.它和onload具有类似的效果 ...
- 第二章 Odoo的安装与部署
Odoo的官方推荐是Ubuntu Server,所以,我们这里也以Ubuntu Server为例.当然,其他系统也是可以的,只不过安装起来相比Ubuntu 会显得稍微折腾,限于篇幅,本书不对其他系统的 ...