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 = 1
    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(10, 70, self.view.frame.size.width-20,self.view.frame.size.height-80) , collectionViewLayout: layout)
        collect.delegate = self
        collect.dataSource = self
        self.view.addSubview(collect)

        collect.backgroundColor = UIColor.whiteColor()
        collect.layer.borderWidth=1

        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=1

        for v in cell.contentView.subviews{
            v.removeFromSuperview()
        }
        let lab = UILabel(frame: CGRectMake(10 , 10 , cell.frame.size.width-20 , cell.frame.size.height-20))
        lab.numberOfLines = 0
        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(250, 50)
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSizeMake(250, 50)
    }
    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=1
        return reusable
    }

    //UICollectionViewDelegateFlowLayout
    //设定collectionView(指定区)的边距
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    }
    //设定指定区内Cell的最小间距,也可以直接设置UICollectionViewFlowLayout的minimumInteritemSpacing属性
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 10
    }
    //设定指定区内Cell的最小行距,也可以直接设置UICollectionViewFlowLayout的minimumLineSpacing属性
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 10
    }
    //设定指定Cell的尺寸
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(200, 100)
    }

    //当指定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.
    }
}
时间: 2024-08-12 23:14:31

UICollectionViewDelegateFlowLayout 使用的相关文章

SDWEBImage和collectionView的组合,以及collectionView的随意间距设置

#import "ViewController.h" #import <ImageIO/ImageIO.h> #import "UIImageView+WebCache.h" @interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> // 数据源 @property

UICollectionView介绍使用

UICollectionView是一种类似于UITableView但又比UITableView功能更强大.更灵活的视图,这是源于它将UICollectionView对cell的布局交给了UICollectionViewLayout,而且允许用户自定义layout来进行布局. 下面是UICollectionView合并内容和布局并生成最终界面的一个流程: 当UICollectionView显示内容时,先从Data source(数据源)获取cell,然后交给UICollectionView.再从U

iOS 微信右上角下拉菜单效果之CMPopTipView,各种角度各种位置

早之前在项目中写了一个类似微信右上角下拉的菜单封装,但是是写死的,当时根本 没有考虑到去其他地方弹出这个东西,虽然看起来弹个窗出来很简单,但是你位子不确 定之后弹出来是有点麻烦的,反正我总是觉得,我们能想到的,老外早就想到了,多给自己弄点工具库,多看看源码,指不定哪天我也搞出一个库来,世界 上有项目经理这种东西,那就没有什么需求是不可能的,各位手头上多准备点工具还是非常有必要的. 先看图: 需求是这样的,点击分类的按钮,竟然不Push到另一个VC去,非要弹个窗出来 但是像我这样的读书人这么有素质

手把手教你使用UICollectionView写公司的项目

公司的UI图 在很多app中都有这样通用的页面,一直没有机会使用UICollectionView,只是简单的看过他的使用方法.今天公司美工出图,使用了他,并且遇到了好多的坑.记录一下过程,不确定使用的方法是不是最优的,如果有更好的方案,一起讨论,一起进步 理论篇 一.UICollectionViewLayout是做什么的? 1.1 在创建UITableView的时候,使用的是- (instancetype)initWithFrame:(CGRect)frame style:(UITableVie

IOS开发之控件篇第二章 - UICollectionViewControllor

1.介绍 UICollectionView和UICollectionViewControllor是IOS6.0后引入的新控件 使用UICollectionView必须实现三个接口: UICollectionViewDataSource UICollectionViewDelegate UICollectionViewDelegateFlowLayout ------------------------------------------------------------------------

UICollectionCell

================= UICollectionView的应用 ================= 用于显示方块Cell 代理:<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> //布局 UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init]; //横向间距 flowLayout.minimumLine

iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置

转载自:http://www.cnblogs.com/tmf-4838/p/5361271.html #import "ViewController.h" #import <ImageIO/ImageIO.h> #import "UIImageView+WebCache.h" @interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICo

iOS之UIColloctionView

iOS--UICollectionView(滚动视图)入门 UICollectionView @interface UICollectionView : UIScrollView UICollectionView 和UICollectionViewController类是iOS6新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似UITableView和UITableViewController类. 使用UICollectionView必须实现 UICollectionVie

iOS--UICollectionView(滚动视图)入门

 UICollectionView @interface UICollectionView : UIScrollView UICollectionView 和UICollectionViewController类是iOS6新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似UITableView和UITableViewController类. 使用UICollectionView必须实现 UICollectionViewDataSource. UICollectionView