import UIKit
//UICollectionViewLayout
//itemSize属性
//设定全局的Cell尺寸,如果想要单独定义某个Cell的尺寸,可以使用下面方法:
// - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//
//minimumLineSpacing属性
//设定全局的行间距,如果想要设定指定区内Cell的最小行距,可以使用下面方法:
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//
//minimumInteritemSpacing属性
//设定全局的Cell间距,如果想要设定指定区内Cell的最小间距,可以使用下面方法:
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
//
//scrollDirection属性
//设定滚动方向,有UICollectionViewScrollDirectionVertical和UICollectionViewScrollDirectionHorizontal两个值。
//
//headerReferenceSize属性与footerReferenceSize属性
//设定页眉和页脚的全局尺寸,需要注意的是,根据滚动方向不同,header和footer的width和height中只有一个会起作用。如果要单独设置指定区内的页面和页脚尺寸,可以使用下面方法:
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//
//sectionInset属性
//设定全局的区内边距,如果想要设定指定区的内边距,可以使用下面方法:
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
//因为UICollectionViewDelegateFlowLayout实际上是UICollectionViewDelegate的一个子协议,它继承了UICollectionViewDelegate,所以只需要在声明处写上UICollectionViewDelegateFlowLayout就行了。
class ViewController: UIViewController , UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout {
let cellIdentity = "cellIdentity"
let cellhead = "cellhead"
let cellfoot = "cellfoot"
let sectioncount =
let datas = ["cell1" , "cell2" , "cell3"]
var collect:UICollectionView! override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title = "image"
self.view.backgroundColor = UIColor.whiteColor() let layout = UICollectionViewFlowLayout()
collect = UICollectionView(frame: CGRectMake(, , self.view.frame.size.width-,self.view.frame.size.height-) , collectionViewLayout: layout)
collect.delegate = self
collect.dataSource = self
self.view.addSubview(collect) collect.backgroundColor = UIColor.whiteColor()
collect.layer.borderWidth= collect.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentity)
collect.registerClass(CollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: cellhead)
collect.registerClass(CollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: cellfoot) } //datasource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return sectioncount
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return datas.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentity, forIndexPath: indexPath)
cell.layer.borderWidth= for v in cell.contentView.subviews{
v.removeFromSuperview()
}
let lab = UILabel(frame: CGRectMake( , , cell.frame.size.width- , cell.frame.size.height-))
lab.numberOfLines =
lab.text = "\(datas[indexPath.item])(\(indexPath.section)-\(indexPath.item))"
cell.contentView.addSubview(lab)
return cell
} //页眉页脚
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSizeMake(, )
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(, )
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) ->UICollectionReusableView {
var reusable:CollectionReusableView! = nil if kind == UICollectionElementKindSectionHeader {
reusable = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: cellhead, forIndexPath: indexPath) as! CollectionReusableView reusable.labText(cellhead)
}else if kind == UICollectionElementKindSectionFooter {
reusable = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: cellfoot, forIndexPath: indexPath) as! CollectionReusableView reusable.labText(cellfoot)
}
reusable.layer.borderWidth=
return reusable
} //UICollectionViewDelegateFlowLayout
//设定collectionView(指定区)的边距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: , left: , bottom: , right: )
}
//设定指定区内Cell的最小间距,也可以直接设置UICollectionViewFlowLayout的minimumInteritemSpacing属性
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{
return
}
//设定指定区内Cell的最小行距,也可以直接设置UICollectionViewFlowLayout的minimumLineSpacing属性
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return
}
//设定指定Cell的尺寸
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(, )
} //当指定indexPath处的item被选择时触发
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("didSelectItemAtIndexPath ++ \(indexPath)")
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

UICollectionViewDelegateFlowLayout 使用的更多相关文章

  1. UICollectionViewFlowLayout & UICollectionViewDelegateFlowLayout

    A concrete layout object that organizes items into a grid with optional header and footer views for ...

  2. 【iOS】Xcode8+Swift3 纯代码模式实现 UICollectionView

    开发环境 macOS Sierra 10.12.Xcode 8.0,如下图所示: 总体思路 1.建立空白的storyboard用于呈现列表 2.实现自定义单个单元格(继承自:UICollectionV ...

  3. UICollectionView布局cell的三种方式

    UICollectionViewFlowLayout里面: // 方法一 - (void)prepareLayout{} // 方法二 - (nullable NSArray<__kindof ...

  4. UI第十九节——UICollectionView

    UICollectionView其实就是UITableView的升级版,在布局方面比UITableView更出色.下面,先看代码吧 #import "RootViewController.h ...

  5. iOS6新特征:UICollectionView介绍

    http://blog.csdn.net/eqera/article/details/8134986 1.1. Collection View 全家福: UICollectionView, UITab ...

  6. iOS瀑布流实现(Swift)

    这段时间突然想到一个很久之前用到的知识-瀑布流,本来想用一个简单的方法,发现自己走入了歧途,最终只能狠下心来重写UICollectionViewFlowLayout.下面我将用两种方法实现瀑布流,以及 ...

  7. iOS开发之窥探UICollectionViewController(五) --一款炫酷的图片浏览组件

    本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectio ...

  8. iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流

    在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...

  9. iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

随机推荐

  1. [LintCode] Left Pad 左填充

    You know what, left pad is javascript package and referenced by React: Github link One day his autho ...

  2. Hadoop.2.x_简单的测试文件读取与上传

    代码如下, 后备参考: package com.bigdata.hadoop.hdfs; import java.io.File; import java.io.FileInputStream; im ...

  3. 全选,不选,反选js

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  4. c#语句 习题

    1.输入月份,日期,打印出是今年的第几天.(今年是平年) 2. 一个游戏,前20关是每一关自身的分数,21-30关每一关是10分,31-40关每一关是20分,41-49关每一关是30分,50关是100 ...

  5. lua库函数

    这些函数都是Lua编程语言的一部分, 点击这里了解更多. assert(value) - 检查一个值是否为非nil, 若不是则(如果在wow.exe打开调试命令)显示对话框以及输出错误调试信息 col ...

  6. steps animation

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Struts基础详解

    1.web.xml配置: <filter> <filter-name>Struts2</filter-name> <filter-class> org. ...

  8. 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列

    最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...

  9. BizTalk开发系列(二十六) 使用Web Service

    Web Service是在构建SOA平台中广泛使用的技术.在BizTalk开发过程中使用SOAP适配器接收和发送 Web Services 请求.业务流程可以发布为 Web Services 并使用外 ...

  10. Cocos2d-x环境搭建

    资源列表 官网上下载最新的cocos2d-x-3.3. 安装JDK,Eclipse,CDT插件,ADT插件. 下载Android SDK,更新.因为国内经常访问不了google,用vpn速度也有点慢. ...