Swift UIcollectionView 最新写法

UIcollectionView现在多多少少写法变得有点不一样了 首先是是 collectionView 的frame设置变得不一样了  以前是collectionView = UICollectionView(frame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)     用的是CGRectMAKE

现在省略了  直接就是CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)

现在直接贴所有最新代码

class ViewController: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    var collectionView:UICollectionView?;


    let CELL_ID = "cell_id";

    let HEAD_ID = "head_id";


    let ScreenHeight = UIScreen.main.bounds.size.height

    let ScreenWidth = UIScreen.main.bounds.size.width



    override func viewDidLoad() {

        super.viewDidLoad();


        self.view.backgroundColor = UIColor.blue;

        self.title = "首页";

        createCollectionView();

    }



    func createCollectionView() {


        let flowLayout = UICollectionViewFlowLayout();


        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), collectionViewLayout: flowLayout)

        collectionView!.backgroundColor = UIColor.orange;

        collectionView!.delegate = self;

        collectionView!.dataSource = self;


        collectionView!.register(TSMHomeCollectionCell.self, forCellWithReuseIdentifier: CELL_ID);

        collectionView!.register(TSMHomeTopCollectionReusable.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HEAD_ID);


        self.view.addSubview(collectionView!);

    }


    //MARK: - UICollectionView 代理


    //分区数

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

        return 3;

    }


    //每个分区含有的 item 个数

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 200;

    }



    //返回 cell

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CELL_ID, for: indexPath) as! TSMHomeCollectionCell;


        return cell;

    }



    //每个分区的内边距

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        return UIEdgeInsetsMake(10, 10, 10, 10);

    }


    //最小 item 间距

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

        return 10;

    }



    //最小行间距

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

        return 10;

    }


    //item 的尺寸

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: ScreenWidth / 4.0 - 50 / 4.0, height: ScreenWidth / 4.0 - 50 / 4.0)

    }


    //每个分区区头尺寸

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

        return CGSize (width: ScreenWidth, height: 40)

    }




    //返回区头、区尾实例

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        var headview = TSMHomeTopCollectionReusable();


        if kind == UICollectionElementKindSectionHeader {


            headview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: HEAD_ID, for: indexPath as IndexPath) as! TSMHomeTopCollectionReusable;


        }


        return headview;

    }



    //item 对应的点击事件

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        print("index is \(indexPath.row)");

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}




TSMHomeCollectionCell.swift里面实现了

class TSMHomeCollectionCell: UICollectionViewCell {

    override init(frame: CGRect) {

        super.init(frame: frame);


        let image = UIImage(named: "head");

        let imageV = UIImageView(frame:CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height));

        imageV.image = image;


        self.addSubview(imageV);


        self.backgroundColor = UIColor.white;

    }


    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

}



TSMHomeTopCollectionReusable.swift实现了


class TSMHomeTopCollectionReusable: UICollectionReusableView {

    override init(frame: CGRect) {

        super.init(frame: frame);


        self.backgroundColor = UIColor.white;

        let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height));

        label.text = "标题";

        self.addSubview(label);

    }


    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder) has not been implemented")

    }

}


虚拟机运行出来的样子


Swift UIcollectionView 最新写法

Swift刚刚入坑的可以看看哦~