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. [转载]窗口之间的主从关系与Z-Order

    窗口之间的主从关系与Z-Order 原文地址:http://www.cnblogs.com/dhatbj/p/3288152.html说明:这是本人2008年写的一篇旧文,从未公开发表过.其中除了一小 ...

  2. webform连接ACCESS数据库

    1.先建立一个名叫mydb.accdb的access数据库 2.他它复制到webform中,放在App_Data文件夹下. 3.在App_Code文件夹下建好封装语句,查询方法,连接语句,其中stud ...

  3. .net 新闻点击量修改,避免恶意刷新

    DataTable dt = bll.GetNewsByID(id);//根据ID获取的新闻详细内容 if (dt != null && dt.Rows.Count > 0) { ...

  4. vmware安装centos时遇到无法创建新虚拟机: 不具备执行此操作的权限。

    我的问题是选择文件位置造成的,我选择在了VMware安装的位置,重新选择一个文件夹即可.

  5. redis发布订阅

    命令 : redis-cli打开一个客户端 Redis Psubscribe 命令订阅一个或多个符合给定模式的频道. 每个模式以 * 作为匹配符,比如 it* 匹配所有以 it 开头的频道( it.n ...

  6. CCF关于NOIP2014复赛报名的通知

    CCF关于NOIP2014复赛报名的通知   CCF NOIP2014复赛全部实行网上注册.报名.未通过网上报名的选手将不具备参赛和申诉资格. 系统注册须知: NOIP2014复赛注册时间:2014年 ...

  7. MyEclipse自定义快捷键

    MyEclipse快捷键设置 分类: JAVA2011-06-30 09:35 11255人阅读 评论(2) 收藏 举报 myeclipseeclipsetriggersmicrosoftjavabi ...

  8. 【iCore3 双核心板_ uC/OS-III】例程九:任务信号量

    实验指导书及代码包下载: http://pan.baidu.com/s/1c1W29uK iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  9. J2EE用户CPU占用过大后的分析过程

        1.找到最耗CPU的java线程ps命令 命令:ps -mp pid -o THREAD,tid,time   或者  ps -Lfp pid 结果展示:            2.可以获取到 ...

  10. Linux下安装vsftpd

    一.安装vsftpd及相关依赖包 #vsftpd安装程序 yum install vsftpd #vsftpd虚拟登陆账户必要依赖包 yum install pam* db4* 安装完之后,vsftp ...