UICollectionView基本使用

学习iOS一段时间了,早听说了UICollectionView的强大一直没有机会使用,今天自己研究了一下。

UICollectonView类似UITableView 用法基本相似, UICollectionView 更灵活,更强大
UICollectonView 将其子视图的位置,大小和外观的控制权委托给一个单独的布局对象。通过提供一个自定义布局对象,你几乎可以实现任何你能想象到得布局。

下面是UICollectionView 常用的代理方法。

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
} -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"CollectionCell"; CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"1680_1050_3983.jpg"];
// [cell sizeToFit];
return cell;
} #pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(, );
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(, , , );
} #pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

下面给出一个实例

首先自定义一个UICollectionViewCell

#import <UIKit/UIKit.h>

@interface CollectionViewCell : UICollectionViewCell

@property (nonatomic, strong)   UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel; @end #import "CollectionViewCell.h" @interface CollectionViewCell() @end @implementation CollectionViewCell - (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor purpleColor];
[self createView];
}
return self;
} - (UIImageView *)imageView {
if (_imageView == nil) {
CGRect frame = CGRectMake(, , , );
_imageView = [[UIImageView alloc] initWithFrame:frame];
}
return _imageView;
} - (UILabel *)titleLabel {
if (_titleLabel == nil) {
CGRect frame = CGRectMake(, , , );
_titleLabel = [[UILabel alloc] initWithFrame:frame];
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
} - (void)createView {
[self.contentView addSubview:self.imageView];
[self.contentView addSubview:self.titleLabel];
} @end

在控制器里设置代理

UICollectionView 初始化是一定要指定布局对象

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

//
// ViewController.m
// CollectionViewController
//
// Created by chao on 15/8/3.
// Copyright (c) 2015年 chao. All rights reserved.
// #import "ViewController.h"
#import "CollectionViewCell.h" @interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation ViewController - (UICollectionView *)collectionView {
if (_collectionView == nil) {
CGRect frame = CGRectMake(, , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
}
return _collectionView;
} - (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
[self.view addSubview:self.collectionView];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} //定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"CollectionCell"; CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"1680_1050_3983.jpg"];
cell.titleLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
// [cell sizeToFit];
return cell;
} #pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(, );
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(, , , );
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
} @end
//
// AppDelegate.h
// CollectionViewController
//
// Created by chao on 15/8/3.
// Copyright (c) 2015年 chao. All rights reserved.
// #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
} @end

(原)UICollectionView的基本使用的更多相关文章

  1. UICollectionView介绍

    文章原出处未知,如有朋友知道,请告诉我,我会补上. 1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView ...

  2. iOS 8自动调整UITableView和UICollectionView布局

    本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...

  3. 通过layout实现可拖拽自动排序的UICollectionView

    文/CenturyGuo(简书作者)原文链接:http://www.jianshu.com/p/8d1bf1838882著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. Translat ...

  4. UICollectionView在Swift3.0中的用法

    UICollectionView在Swift3.0中的用法 UICollectionView的初始化跟OC中是相似的,创建 GameView 集成自 UICollectionView .注意不同于UI ...

  5. UICollectionView Demo

    1. 利用系统自动布局UICollectionViewFlowLayout进行布局. ViewController1 #import "ViewController1.h" @in ...

  6. UICollectionView官方使用示例代码研究

    注:这里是iOS6新特征汇总贴链接 iOS6新特征:参考资料和示例汇总 这个链接可以学习到UICollectionView的相关介绍:iOS6新特征:UICollectionView介绍 由于UICo ...

  7. [转载]iOS6新特征:UICollectionView官方使用示例代码研究

    原文地址:iOS6新特征:UICollectionView官方使用示例代码研究作者:浪友dans 注:这里是iOS6新特征汇总贴链接 iOS6新特征:参考资料和示例汇总 这个链接可以学习到UIColl ...

  8. [转]iOS8 自动调整UITableView和UICollectionView布局

    转自:http://www.cocoachina.com/industry/20140825/9450.html (via:玉令天下的Blog)   本文讲述了UITableView.UICollec ...

  9. UICollectionView与UITableView混用手势冲突

    前言 最近在重构某个模块,以后别人封装的所谓的基类就像一坨死一样,看见就恶心,相信同行的你们能够明白那种心情.为什么要重构?并不是真的因为它像一坨死,而是因为这个模块是用户使用最频繁的,而且出现了不少 ...

随机推荐

  1. UVa12298(生成函数的简单应用+FFT)

    I have a set of super poker cards, consisting of an infinite number of cards. For each positive compo ...

  2. python里面的list、tuple和dict的区别

    Dictionary .Dictionary是Python中内置的数据类型之一,他定义了键和值之间一对一的关系. 每一个元素都有一个key-value对,整个元素集合用大括号{}括起来. 你可以通过k ...

  3. iis日志存放位置 及 查看方法

    IIS:控制面板--管理工具--internet信息服务 网站的IIS日志是在空间里面看的.要登陆到空间里面的一个IIS日志里面看.IIS日志一般都很大的.看会有点.. 一.应用程序日志.安全日志.系 ...

  4. nginx 日志 log_format 及字段说明

    1.log_format 普通格式 log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$stat ...

  5. CentOS远程监控

    近日,因工作需要,学习了CentOS远程监控的水平有限,多指教. 远程访问CentOS,包括三种方式ssh,telnet,vnc. 本例涉及的是以vnc远程访问CentOS.指令在root下操作.注意 ...

  6. GIMP 无法设置中文的问题解决

    首先按照网上说的安装了language-pack-gnome-zh-hant 参考链接:http://www.ubuntu-tw.org/modules/newbb/viewtopic.php?top ...

  7. js实现接口的几种方式

    Javascript模仿接口可以有三种方式:1.注释法 2.检查属性法 3.鸭式辨形法 1.注释法:此方法属于程序文档范畴,对接口的继承实现完全依靠程序员自觉 /* interface People{ ...

  8. STL使用迭代器逆向删除

    网上有很多这种例子: void erase(vector<int> &v) { for(vector<int>::reverse_iterator ri=v.rbegi ...

  9. 从零开始利用vue-cli搭建简单音乐网站(三)

    1.利用router-link在组件之间传递数据 如上图,MainPage.vue中主要有8个推荐曲目数据,主要实现方式是建立好主页面模板,然后用v-for循环获取返回的music对象,然后分别绑定曲 ...

  10. setuid

    -r-s--x--x   #s就是setuid,仅可用在二进制文件,对目录设置无效