UIColletionView

这篇只是做UIColletionView的常用属性、代理方法和数据源方法的备忘,之后做一些自定义布局,增加删除动画等。

UIColletionViewFlowLayout的常用属性和UIColletionView的常用属性方法

        // 创建布局类
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSizeMake(50.0, 50.0)
layout.headerReferenceSize = CGSizeMake(view.frame.size.width, 100)
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
layout.minimumLineSpacing = 50.0
layout.minimumInteritemSpacing = (view.frame.size.width - 20 - 50 * 4) / 4
layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10) // 创建collection view
let collectionView = UICollectionView(frame: CGRectMake(0, 20, view.frame.size.width, view.frame.size.height - 20), collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.whiteColor()
collectionView.delegate = self
collectionView.dataSource = self // 注册Cell SectionHeader SectionFooter
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader")
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SectionFooter") view.addSubview(collectionView)

一般简单的布局使用原生的flowlayout就行,常用属性:

  • itemSize:每个cell的尺寸。
  • headerReferenceSize:组头的尺寸。
  • footerReferenceSize:组尾的尺寸。
  • scrollDirection:滚动方向。
  • minimumLineSpacing:每一行的距离。
  • minimumInteritemSpacing:每个cell之间的距离。
  • sectionInset:组的内边距。

一般collection view的必须设置的属性和方法:

  • delegate:代理,可以监听事件。
  • dataSource:数据源,提供一些collection view要用的数据
  • registerClass(_, forCellWithReuseIdentifier:):注册Cell,实现复用机制。
  • registerClass(_, forSupplementaryViewOfKind: , withReuseIdentifier:):注册组头或组尾,也是可以复用的。

Delegate

高亮:

    // cell是否允许高亮
func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // cell已经进入高亮状态
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
print("didHighlightItemAtIndexPath")
} // cell已经结束高亮状态
func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
print("didUnhighlightItemAtIndexPath")
}

选中:

    // cell是否允许选中
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // cell是否允许取消选中
func collectionView(collectionView: UICollectionView, shouldDeselectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // cell被选中
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("didSelectItemAtIndexPath")
} // cell取消选中
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
print("didDeselectItemAtIndexPath")
}

显示(iOS8):

    // 即将有cell显示
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
print("willDisplayCell")
} // 即将有组头或组尾显示
func collectionView(collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, atIndexPath indexPath: NSIndexPath) {
print("willDisplaySupplementaryView")
} // 有cell不在屏幕上显示了
func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
print("didEndDisplayingCell")
} // 有组头或组尾不在屏幕上显示了
func collectionView(collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, atIndexPath indexPath: NSIndexPath) {
print("didEndDisplayingSupplementaryView")
}

菜单:

    // 是否允许显示菜单
func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
} // 允许实现那个菜单方法
func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return true
} // 菜单方法的具体实现
func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) { }

重新布局 :

    // 重新布局的时候调用
func collectionView(collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout {
print("transitionLayoutForOldLayout")
let transitionLayout = UICollectionViewTransitionLayout(currentLayout: fromLayout, nextLayout: toLayout)
transitionLayout.transitionProgress = 1.5
return transitionLayout
}

DelegateFlowLayout

    // 设置每个cell的尺寸
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if indexPath.row % 2 == 0{
return CGSizeMake(30, 30)
} else {
return CGSizeMake(50, 50)
}
} // 设置每一组的内边距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(20, 5, 10, 5)
} // 返回每一组的行距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10
} // 返回每组item之间的间距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10
} // 返回每一组的组尾尺寸
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSizeMake(view.frame.size.width, 80)
} // 返回每一组的组头尺寸
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(view.frame.size.width, 200)
}

DataSource

    // 必须实现 返回cell个数
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 40
} // 必须实现 返回每个cell 复用机制
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.brownColor() return cell
} // 返回有几组
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 2
} // 返回组头或者组尾视图,同样用复用机制
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let reusableView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "SectionHeader", forIndexPath: indexPath)
if kind == UICollectionElementKindSectionHeader {
reusableView.backgroundColor = UIColor.blackColor()
} else {
reusableView.backgroundColor = UIColor.blueColor()
}
return reusableView
}

Colletion View 简单的备忘的更多相关文章

  1. sql简单学习---备忘

    2:Hibernate public boolean deleteFlowerById(int id) { Session session = sessionFactory.openSession() ...

  2. centos简单命令备忘

    1.查看当前目录 [root@instance-aif30iaj bin]# df -h 2.查看指定目录 在命令后直接放目录名,比如查看"usr"目录使用情况: [root@in ...

  3. Table view 备忘

    Table view 备忘 本篇会以备忘为主,主要是一些基础的代理方法和数据源方法具体的优化好点子会后续跟上. Table view的数据源方法 必须实现的数据源方法 // 返回每一行的cell,可以 ...

  4. Scroll view 备忘

    Stroyboard中使用ScrollView 当我们使用Storyboard开发项目时,如果要往控制器上拖入一个ScrollView并且添加约束设置滚动区域,是有特殊的规定的: 拖入一个scroll ...

  5. 个人 WPF+EF(DBFirst) 简单应用开发习惯及EF学习测试(备忘) -- 2

    接上篇:个人 WPF+EF(DBFirst) 简单应用开发习惯及EF学习测试(备忘) -- 1 Step1 在主程序中设置连接数据库 从Model类库的 App.Config 把数据库字符串拷贝出来, ...

  6. Spring boot 注解简单备忘

    Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...

  7. 简单备忘一下Linux下的wget和curl如何使用http proxy

    简单备忘一下Linux下的wget和curl如何使用http proxywget -e "http_proxy=porxyhost:port" www.baidu.comcurl ...

  8. 基于Prism.Windows的UWP开发备忘

    以前做UWP开发都是使用MvvmLight,主要是简单易上手,同时也写了很多MvvmLight的开发系列文章: UWP开发必备以及常用知识点总结 UWP开发之Mvvmlight实践九:基于MVVM的项 ...

  9. AngularJS之备忘与诀窍

    译自:<angularjs> 备忘与诀窍 目前为止,之前的章节已经覆盖了Angular所有功能结构中的大多数,包括指令,服务,控制器,资源以及其它内容.但是我们知道有时候仅仅阅读是不够的. ...

随机推荐

  1. Unity3D中Prefab

    Prefab概念: Prefab是一种资源类型--存储在项目视图中的一种可反复使用的游戏对象.因而当游戏中须要非常多反复使用的对象.资源等时,Prefab就有了用武之地.它拥有下面特点: 能够放到多个 ...

  2. android 68 单元测试

    package com.itheima.junit; import android.os.Bundle; import android.app.Activity; import android.vie ...

  3. android 62 手机存储目录的划分

    android下应用程序的路径和javase不同,应用程序的数据要保存自己的文件夹里面 > > getFileDir(); 获取自己的文件夹 /data/data/包名(应用程序的名字)/ ...

  4. bash if 表达式

    .bash把[[ $a -lt $b ]]看作一个单独的元素,并且返回一个退出码.退出码0为真,非零为假 例如: a= b=c [[ $a -lt $b ]] echo $? # a小于b为真 [[ ...

  5. PureMVC(JS版)源码解析(九):View类

    在讲解View类之前,我们先回顾一下PureMVC的模块划分:      在PureMVC中M.V.C三部分由三个单例类管理,分别是Model/View/Controller.PureMVC中另外一个 ...

  6. XPath操作XML文档

    NET框架下的Sytem.Xml.XPath命名空间提供了一系列的类,允许应用XPath数据模式查询和展示XML文档数据. 3.1XPath介绍 主要的目的是在xml1.0和1.1文档节点树种定位节点 ...

  7. json 是什么

    怎么学习一个知识? 首先要提出几个问题,目前认为json是个什么,json是谁创造的,为什么而出现的,但是目前仅有很长时间之前别人直接告诉我的,json用来存数据的,对于使用也忘记的差不多了,所以现在 ...

  8. 3、bootstrap3.0 栅格偏移 布局中的一个特产

    理解了栅格化,那么栅格偏移也相对容易理解了.v3的偏移分别有以下几种: offset:左外边距(margin-left): pull:右位移(right): push:左位移(left). 其中off ...

  9. C# Ref 与out 的区别

    在C#中,有四种传递参数方式: 1. 传值 (value) : 无额外修饰符 2. 传址(reference) : 需修饰符Ref,传入函数的参数必须先赋值 3. 输出参数(output): 需修饰符 ...

  10. hadoop_并行写操作思路_2

    如果想实现将 Client端的 File并行写入到 各个Datanode中, 首先, 应该修改的是,DistributedFileSystem中的create方法, 在create 内部调用FSNam ...