zoukankan      html  css  js  c++  java
  • 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.
        }
    }
  • 相关阅读:
    [USACO13NOV] Pogo-Cow
    《高性能Mysql》讲聚簇索引
    复合索引底层实现
    数据库索引实现(B+,B-,hash)
    B+树,B树,聚集索引,非聚集索引
    MySQL存储引擎
    synchronized实现原理
    【1】线程池的使用
    CompletionService
    原型模式
  • 原文地址:https://www.cnblogs.com/tangyuanby2/p/6097127.html
Copyright © 2011-2022 走看看