UICollectionViewDelegateFlowLayout 使用
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 使用的更多相关文章
- UICollectionViewFlowLayout & UICollectionViewDelegateFlowLayout
A concrete layout object that organizes items into a grid with optional header and footer views for ...
- 【iOS】Xcode8+Swift3 纯代码模式实现 UICollectionView
开发环境 macOS Sierra 10.12.Xcode 8.0,如下图所示: 总体思路 1.建立空白的storyboard用于呈现列表 2.实现自定义单个单元格(继承自:UICollectionV ...
- UICollectionView布局cell的三种方式
UICollectionViewFlowLayout里面: // 方法一 - (void)prepareLayout{} // 方法二 - (nullable NSArray<__kindof ...
- 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瀑布流实现(Swift)
这段时间突然想到一个很久之前用到的知识-瀑布流,本来想用一个简单的方法,发现自己走入了歧途,最终只能狠下心来重写UICollectionViewFlowLayout.下面我将用两种方法实现瀑布流,以及 ...
- iOS开发之窥探UICollectionViewController(五) --一款炫酷的图片浏览组件
本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectio ...
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...
随机推荐
- hdu Dragon Balls
这题是一道简单的并查集的运用.龙珠所在的城市.该城市龙珠数目都是很简单的问题,稍微麻烦一点的就是龙珠被移动的次数,因为每一次要移动的是一个城市中所有的龙珠,所以每次移动该城市中所有龙珠的移动次数都要加 ...
- VC 解密OUTLOOK pop3保存注册表密码
原文连接:https://forum.90sec.org/forum.php?mod=viewthread&tid=8410 作者:Agile 用过OUTLOOK的人都知道,OUTLOOK的密 ...
- 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript...”要求对象引用 (封装注册脚本)
在写项目时想对asp.net的注册前台脚本事件进行封装,就添加了一个BasePage.cs页面,但一直报错‘非静态的字段.方法或属性“System.Web.UI.Page.ClientScript.. ...
- Android课程---日历选择器和时间选择器
package com.hanqi.test5; import android.os.Bundle; import android.support.annotation.IdRes; import a ...
- Json的语法及使用方法
Json的语法及使用方法 Json(JavaScript Object Notation)对象表示标识,是一种轻量级的数据交换语言,比XML更容易解析,独立于语言和平台. 语法规则: 对象用{}保存 ...
- html中select只读显示
因为Select下拉框只支持disabled属性,不支持readOnly属性,而在提交时,disabled的控件,又是不提交值的.现提供以下几种解决方案: 1.在html中使用以下代码,在select ...
- .gitignore文件不起作用
当我们用git时常常会习惯把我们不想上传到远程代码库中的一些本地文件(夹)放在一个叫做.gitignore的文件中,例如常见的本地build文件夹,一些IDE如Intellig,Eclipse的项目管 ...
- 【转】ArcGIS 创建切片缓存方法工具总结
ArcGIS 创建切片缓存方法工具总结 http://wenku.baidu.com/link?url=Bm8AkmcJBzfiyat9N_Me6vlfSHEDCC_D1qBk5IB4X4CIDeKI ...
- python简单爬虫示例
#coding=utf-8 import urllib import re def downloadPage(url): h = urllib.urlopen(url) ret ...
- 线性探测再散列 建立HASH表
根据数据元素的关键字和哈希函数建立哈希表并初始化哈希表,用开放定址法处理冲突,按屏幕输出的功能表选择所需的功能实现用哈希表对数据元素的插入,显示,查找,删除. 初始化哈希表时把elem[MAXSIZE ...