UICollectionView的基本使用
这个控件,看起来与UITableView有点像,而且基本的用法也很相像哦!!!
我们来看看API:
- #pragma mark - UICollectionViewDataSource
- // 指定Section个数
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 3;
- }
- // 指定section中的collectionViewCell的个数
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return 10;
- }
- // 配置section中的collectionViewCell的显示
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
- cell.backgroundColor = [UIColor redColor];
- cell.textLabel.text = [NSString stringWithFormat:@"(%ld %ld)", indexPath.section, indexPath.row];
- return cell;
- }
看看直线布局的API:
- #pragma mark - UICollectionViewDelegateFlowLayout
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.width / 3 - 10);
- }
- // 设置每个cell上下左右相距
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- return UIEdgeInsetsMake(5, 5, 5, 5);
- }
- // 设置最小行间距,也就是前一行与后一行的中间最小间隔
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- return 10;
- }
- // 设置最小列间距,也就是左行与右一行的中间最小间隔
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
- return 10;
- }
- // 设置section头视图的参考大小,与tableheaderview类似
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
- return CGSizeMake(self.view.frame.size.width, 40);
- }
- // 设置section尾视图的参考大小,与tablefooterview类似
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
- return CGSizeMake(self.view.frame.size.width, 40);
- }
如果是固定的,可以使用全局属性:
- NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewFlowLayout : UICollectionViewLayout
- @property (nonatomic) CGFloat minimumLineSpacing;
- @property (nonatomic) CGFloat minimumInteritemSpacing;
- @property (nonatomic) CGSize itemSize;
- @property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -perferredLayoutAttributesFittingAttributes:
- @property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical
- @property (nonatomic) CGSize headerReferenceSize;
- @property (nonatomic) CGSize footerReferenceSize;
- @property (nonatomic) UIEdgeInsets sectionInset;
- @end
常用到的代理方法:
- #pragma mark - UICollectionViewDelegate
- // 允许选中时,高亮
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
- // 高亮完成后回调
- - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
- // 由高亮转成非高亮完成时的回调
- - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
- // 设置是否允许选中
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
- // 设置是否允许取消选中
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
- // 选中操作
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
- // 取消选中操作
- - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
demo:https://github.com/632840804/CollectionViewDemo
UICollectionView的基本使用的更多相关文章
- 【iOS】Xcode8+Swift3 纯代码模式实现 UICollectionView
开发环境 macOS Sierra 10.12.Xcode 8.0,如下图所示: 总体思路 1.建立空白的storyboard用于呈现列表 2.实现自定义单个单元格(继承自:UICollectionV ...
- 使用UICollectionView实现首页的滚动效果
实现类似这样的效果,可以滚动大概有两种实现方案 1. 使用scrollview来实现 2. 使用UICollectionView来实现 第一种比较简单,而且相对于性能来说不太好,于是我们使用第二种方案 ...
- 用NSCalendar和UICollectionView自定义日历,并实现签到显示
前一段时间因为工作需要实现了一个可以签到的日历,来记录一下实现的思路 效果如图: 这里的基本需求是: 1,显示用户某个月的签到情况,已经签到的日子打个圈,没有签到且在某个时间范围内的可以签到,其他 ...
- UICollectionView布局cell的三种方式
UICollectionViewFlowLayout里面: // 方法一 - (void)prepareLayout{} // 方法二 - (nullable NSArray<__kindof ...
- 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱
前言 API 不熟悉导致的问题,想当然的去理解果然会出问题,这里记录一下 UICollectionView 使用问题. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cn ...
- UICollectionLayout布局 —— UIKit之学习UICollectionView记录二《流水布局》
重点知识 一. 加载collectionView注意事项 1.创建collectionView,有两种方式 :一种是xib和一种是纯代码:设置代理和数据源,注册cell,配置流水布局的属性,如上.下. ...
- UICollectionView中使用 UICollectionViewFlowLayout进行布局(模仿苹果相册)
现在都知道,在初始化UICollectionView的时候,必须要传入一Layout对象,进行布局管理.这也是collectionview和tableview的明显区别,通过collectionvie ...
- UI第十九节——UICollectionView
UICollectionView其实就是UITableView的升级版,在布局方面比UITableView更出色.下面,先看代码吧 #import "RootViewController.h ...
- iOS6新特征:UICollectionView介绍
http://blog.csdn.net/eqera/article/details/8134986 1.1. Collection View 全家福: UICollectionView, UITab ...
- 【iOS】UITabView/UICollectionView 全选问题
UITabView/UICollectionView 全选问题 SkySeraph July. 30th 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySera ...
随机推荐
- Python的学习
1.psutil的安装 [root@YQY-TIAN- ~]# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.t ...
- MVC,jquery异步
创建一个Ajax控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...
- WPF样式和资源2
<Window.Resources> <FontFamily x:key="ButtonFontFamily">Time New Roman</Fon ...
- 报错: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is ins
环境:Xcode7.1.1 + iOS9.1 详细错误: App Transport Security has blocked a cleartext HTTP (http://) resource ...
- TCP/IP 要点备忘
1. 3次握手/4次挥手过程,以及状态变化: 2. RTT,TTL,TOS(8位服务类型,最小延时.最大吞吐.最高可用.最小费用). 3. TimeWait(2msl)状态,防止最后一个ack丢失 4 ...
- smarty 基本介绍及示例
什么是smarty? Smarty是一个使用PHP写出来的模板引擎,是业界最著名的PHP模板引擎之一.Smarty分离了逻辑代码和外在的内容,提供一种易于管理和使用的方法,用来将原本与HTML代码混杂 ...
- Vim的snipMate插件
介绍终于发现了一个插件,对于Vim下代码块的自动补全支持的很好.给大家推荐snipMate. snipMate可以帮助您在vim上实现类似Textmate的功能,自动代码块的能力非常强大,而且代码块是 ...
- Git学习笔记01--初始化设置
1.查看git版本 $ git --version 2.设置用户姓名和邮箱 $ git config --global user.name “Craftor” $ git config --globa ...
- Java学习笔记--HashMap中使用object做key的问题【转】
在HashMap中,如果需要使用多个属性组合作为key,可以将这几个属性组合成一个对象作为key.但是存在的问题是,要做get时,往往没办法保存当初put操作时的key object的referenc ...
- NSNotificationCenter基础知识
NSNotificationCenter基础知识 Notification的工作机制 1.对应用程序中其他地方发生的事件(比如增加一条数据库记录)感兴趣的对象,会向通告中心(Notificatio ...