swift 之UICollectionView 与UICollectionViewController

来源:互联网 发布:unitcell软件 编辑:程序博客网 时间:2024/06/08 00:54

网格属性

1、layout 影像控件的外观,支持Flow、custom两种布局方式  选择流布局会增加一个scrolDirection属性,用来执行滚动方向是横向还是纵向

2、Accessories 是否显示页眉和页脚,在后一示例中我们会看到添加页眉页脚的效果。

3、各种尺寸属性

Cell Size:单元格尺寸

Header Size:页眉尺寸

Footer Size:页脚尺寸

Min Spacing:单元格之间间距

section Insets :各方能去上下左右空白区域大小

简单的流布局

import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    var controller : UICollectionView!
    
    let course = [["name":"swift","pic":"icon"],["name":"swift","pic":"icon"],["name":"swift","pic":"icon"],["name":"swift","pic":"icon"]]
    
 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize.init(width: 80, height: 80)
        //列间距,行间距,偏移
        layout.minimumInteritemSpacing = 15
        layout.minimumLineSpacing = 30
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
       self.controller = UICollectionView.init(frame: self.view.frame, collectionViewLayout: layout)
        self.controller.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        self.controller.backgroundColor = UIColor.gray
        self.view.addSubview(self.controller)
        self.controller.delegate = self
        self.controller.dataSource = self
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return course.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
       cell.backgroundColor = UIColor.red
        return cell
    }
    
        
   
}

0 0