swift中collectionView的用法

  • collectionView是iOS中一個(gè)非常重要的空間,但是使用的頻率可能不高,方法相對(duì)來(lái)說(shuō)和tableview有所不同,記錄一下,防止遺忘
  • 1.創(chuàng)建UICollectionViewLayout
  • 2.創(chuàng)建UICollectionView
  • 3.設(shè)置數(shù)據(jù)源方法
  • 4.設(shè)置代理方法
  • 5.注冊(cè)cell
1.創(chuàng)建UICollectionViewLayout
  • 過(guò)去一直認(rèn)為collectionView比較難使用,尤其是UICollectionViewLayout,不知道該怎么設(shè)置,后來(lái)突然想到了,其實(shí)UICollectionViewLayout和tableview的UITableViewStyle.Plain是一樣的,就是一個(gè)必設(shè)的屬性。只不過(guò)**UICollectionViewLayout **是具體的item的大小,樣式,間距等等屬性,我們要用具體的flow實(shí)例化對(duì)象
     private let flowLayout : UICollectionViewFlowLayout = {
        let flt = UICollectionViewFlowLayout()
        flt.minimumLineSpacing = 0
        flt.minimumInteritemSpacing = 0
        flt.scrollDirection = UICollectionViewScrollDirection.Horizontal
        flt.itemSize = UIScreen.mainScreen().bounds.size
        return flt
    }()
2.創(chuàng)建UICollectionView
  • 因?yàn)槲艺J(rèn)為,collectionView的生成,他要變成什么樣子的,他自己最清楚,所以就講flowLayout的的創(chuàng)建全部放到collectionViewController的內(nèi)部
    init(){   
        super.init(collectionViewLayout:flowLayout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

**init 之所以沒(méi)有去寫(xiě)override,是因?yàn)閏ollectionVC默認(rèn)的方法是init(collectionViewLayout layout: UICollectionViewLayout) **

設(shè)置一下collectionView的基本屬性,可以分頁(yè),和橫向滑動(dòng),豎直方法滑動(dòng)

        self.collectionView!.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
3.設(shè)置數(shù)據(jù)源方法

如果vc繼承于UICollectionViewController的話,extension方法這樣寫(xiě)

extension WXNewFeatureController{
    
    // MARK: UICollectionViewDataSource
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! WXNewFeatureCell
        cell.imageIndex = indexPath.item
        return cell
    }
}
4.設(shè)置代理方法

代理方法可以寫(xiě),也可以不寫(xiě)~~

5.注冊(cè)cell

如果懶得自定義就用系統(tǒng)的

  //聲明一個(gè)常量

private let reuseIdentifier = "Cell"
//去注冊(cè)
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(WXNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
//調(diào)用
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
        return cell
    }
6.自定義cell

因?yàn)橐恢绷?xí)慣封裝,而且可以各種自定義內(nèi)部控件,所以就去封裝一下,這個(gè)cell就是在這個(gè)collectionViewController中使用,所以我么就將這個(gè)cell的創(chuàng)建放到這個(gè)文件中,而且創(chuàng)建可以使用private修飾,方法也可以用它修飾,在這個(gè)文件中,Private修飾的都可以隨意調(diào)用,哪怕不是同一個(gè)類(lèi)都行

private class WXNewFeatureCell:UICollectionViewCell{
    //對(duì)外開(kāi)放一個(gè)index屬性
    var imageIndex : NSInteger? {
        didSet{
           imageView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
            lab.text = "new_feature_\(imageIndex! + 1)"
            //判斷如果是第4個(gè)頁(yè)面,我們就去添加一個(gè)按鈕
            if imageIndex! + 1 == 4 {
                confirmBtn.hidden = false
                contentView.addSubview(confirmBtn)
            }
        }
    }
    //懶加載數(shù)據(jù)
    private lazy var imageView : UIImageView = {
         return UIImageView()
    }()
    
    private lazy var lab : UILabel = UILabel()
  
    private lazy var confirmBtn:UIButton =  {
       let btn = UIButton()
        btn.hidden = true
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        return btn
    }()
      //MARK:-自定義一個(gè)collctionView
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //創(chuàng)建子控件
  private func setupUI(){
   contentView.addSubview(imageView)
    contentView.addSubview(lab)
//創(chuàng)建完子控件,必須調(diào)用這個(gè)方法,否則不去調(diào)用布局方法
    setNeedsUpdateConstraints()
    }
    //初始化的時(shí)候,如果不去調(diào)用的話,不走這個(gè)方法,很惡心,所以初始化完畢一定要去調(diào)用一下
    override func updateConstraints() {
        super.updateConstraints()
        imageView.autoPinEdgesToSuperviewEdges()
        lab.autoPinEdgesToSuperviewEdges()
        if imageIndex == 3 {
            let size = confirmBtn.currentBackgroundImage?.size
            confirmBtn.autoSetDimensionsToSize(size!)
            confirmBtn.autoAlignAxisToSuperviewAxis(ALAxis.Vertical)
            confirmBtn.autoPinEdgeToSuperviewEdge(ALEdge.Bottom, withInset: 100)
        }
    }
    
}
7.flowLayout可以的自定義

我太懶,就不寫(xiě)了,可以在這個(gè)文件中寫(xiě),沒(méi)關(guān)系~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容