iOS开发 纯代码创建UICollectionView
转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html
iOS开发 纯代码创建UICollectionView
习惯了使用xib和StoryBoard创建UICollectionView项目工程的伙伴,需要转换使用纯代码来实现,想避免碰更多的壁,就需要认真了解创建UICollectionView过程了。创建UICollectionView比创建UITableView更加复杂,初始化方式也是相对奇特。以下是使用纯代码创建UICollectionView的方法。
工具/原料
- MAC OS X操作系统
- Xcode编译器
方法/步骤
-
1
创建工程项目和视图控制器
创建工程项目UICollectionView,新建一个UIViewController。选中工程,右键-New File…选择“Cocoa Touch Class”-Next,给个合理的名称ViewController,再Next完成。
在AppDelegate.m文件包含#import "ViewController.h"。添加代码:
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
self.window.rootViewController = navC;//将navC设置为根视图控制器。
修改一下ViewController的显示样式,执行编译,run一下,效果如图。
-
2
创建自定义UICollectionViewCell
选中工程,右键-New File…选择“Cocoa Touch Class”-Next,选择继承于UICollectionViewCell类,给个合理的名称CollectionViewCell,再Next完成。
1、自定义所需要的控件,比如UIImageView:
@property(nonatomic ,strong)UIImageView *imgView;
2、初始化控件,在方法- (id)initWithFrame:(CGRect)frame中实现:
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 30, 150, 140)];
self.imgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self addSubview:self.imgView];
-
3
创建UICollectionView及添加代理
1、在ViewController.h添加事件代理和数据源代理<UICollectionViewDataSource,UICollectionViewDelegate>。
2、在ViewController.m创建UICollectionView。需要使用UICollectionViewFlowLayout来创建,使用方法- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;如果只用普通的init方法,是实现不了的。
4、代理授权并添加至视图。
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];
-
4
把UICollectionViewCell添加到UICollectionView内
1、注册CollectionViewCell,添加cell需要在这里实现。方法:- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
2、添加代理方法
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
-
5
设置UICollectionView中的属性
//定义每个UICollectionView 的大小(返回CGSize:宽度和高度)
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//定义每个UICollectionView 的间距(返回UIEdgeInsets:上、左、下、右)
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
END
代码下载地址
- View requires API level 14 (current min is 8)
- UESTC 250 windy数
- 基于SSH框架的EasyUI的前端DataGrid实现
- LeetCode-63-Unique Paths II(动规)-Medium
- php关联数组通过key获取value
- UESTC 758 P酱的冒险旅途
- 如何得到Ubuntu UI Toolkit的版本
- POJ 3061 Subsequence (尺取)
- IOS NSOperation&NSOperationQueue
- 如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]
- UGUI动态改变Grid大小
- UVA 11538Chess Queen
- 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!
- 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!
- 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!
- iOS开发笔记12:iOS7上UITextField限制字数输入导致崩溃问题
- Leetcode Longest Consecutive Sequence
- UIWindow的研究
- UIGraphicsBeginImageContext
- UITableView性能优化