swift学习 - collectionView

swift CollectionView学习

效果图:

源码:
ContModel.swift

import UIKit

class ContModel: NSObject {

    var title:String?
    var image:String?
    
    func setValue(value: AnyObject?, forUndefinedKey key: String) {
    
    }

}

ContViewCell.swift
cell截图

import UIKit

class ContViewCell: UICollectionViewCell {

    @IBOutlet weak var img: UIImageView!
    @IBOutlet weak var titleLb: UILabel!
    
    var model:ContModel?{
        //重写set方法
        didSet{
            img.image = UIImage(named: model!.image! )
            titleLb.text = model?.title
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.layer.cornerRadius = 10
        self.clipsToBounds = true
    }
    
}

ViewController.swift

import UIKit

class ViewController: UIViewController {
 
    var collectionView:UICollectionView?
    var dataArr:NSMutableArray?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        prepareData()
        setupCollectionView()
    }
    
    func prepareData() {
        dataArr = NSMutableArray()
        for i in 0..<7
        {
            let model = ContModel()
            model.image = "image\(i)"
            model.title = "xxx\(i)"
            dataArr?.add(model)
        }
    }

    func setupCollectionView() {
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: 300, height: 400)
//        layout.minimumLineSpacing = 5.0
        layout.minimumInteritemSpacing = 10.0
        let col = UICollectionView(frame:CGRect(x: 0, y: 100, width: 375, height: 400), collectionViewLayout: layout)
        col.dataSource = self
        col.backgroundColor = .clear;
        col.register(UINib.init(nibName: "ContViewCell", bundle: nil), forCellWithReuseIdentifier: "ContViewCell")
        collectionView = col
        view.addSubview(col)
    }
    
}

extension ViewController:UICollectionViewDataSource
{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (dataArr?.count)!
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContViewCell", for: indexPath) as! ContViewCell
        let model = dataArr?[indexPath.row]
        cell.model = model as! ContModel?
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 200, height: 200)
    }
    
    
}

collectionView的方法和objc中的使用方法类似
这几有几个重点需要注意
1.使用UIVisualEffectView(iOS8以后才有的View)给view添加Effective效果
2.怎么自定义uicollectionViewCell
3.怎么创建自定义model,并重写set方法

posted @ 2017-03-06 16:11  qqcc1388  阅读(258)  评论(0编辑  收藏  举报