手动添加代理人和数据源到xib文件的UICollectionView

时间:2018-02-06 06:31:31

标签: ios swift xcode swift4 xcode9



我把UICollectionView放在xib文件中的UIView上 在xib文件中安装了UICollectionView(GUI),并且连接了IBOutlet。

在这种情况下,我们应该如何编写delegate和dataSource?
(猜想:也许,我必须自己手动编写代码)

另外,我们无法在xib中将Cell附加到UICollectionView 这是否意味着我不必提出它?

Collection.swift

//Collection.swift

import UIKit

class Home: UIView, UICollectionViewDelegate, UICollectionViewDataSource{

    @IBOutlet weak var propCollectionView: UICollectionView!

    let photos = ["sierra"] //Already put to AssetsFolder

    //Just instance. plz do not care about this.
    class func instance() -> Home {
        return UINib(nibName: "Home", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! Home
    }

    func initValue(){
        propCollectionView.register(UINib(nibName: "PropCell", bundle: nil), forCellWithReuseIdentifier: "PropCell")
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell        = propCollectionView.dequeueReusableCell(withReuseIdentifier: "PropCell", for: indexPath) as! PropCell
        let propImage   = cell.contentView.viewWithTag(1) as! UIImageView
        let img         = UIImage(named: photos[indexPath.row])
        propImage.image = img
        return cell
    }
}



PropCell.swift

//PropCell.swift
//yeah, nothing. plz don't care this too.

import UIKit

class PropCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

    }

}



CollectionSwift的Xib
Image



PropCell的Xib
Image

(如果您不理解问题的含义,请发表评论。)

1 个答案:

答案 0 :(得分:0)

import UIKit

class Home: UIView, UICollectionViewDelegate, UICollectionViewDataSource{

    @IBOutlet weak var propCollectionView: UICollectionView!

    let photos = ["sierra"] //Already put to AssetsFolder

    //Just instance. plz do not care about this.
    class func instance() -> Home {
        return UINib(nibName: "Home", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! Home
    }
    func configureView() {
        propCollectionView.delagate = self
        propCollectionView.dataSource = self
    }

    func initValue(){
        propCollectionView.register(UINib(nibName: "PropCell", bundle: nil), forCellWithReuseIdentifier: "PropCell")
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell        = propCollectionView.dequeueReusableCell(withReuseIdentifier: "PropCell", for: indexPath) as! PropCell
        let propImage   = cell.contentView.viewWithTag(1) as! UIImageView
        let img         = UIImage(named: photos[indexPath.row])
        propImage.image = img
        return cell
    }
}

<强>用途

let home = Home.instance()
home.configureView() //called configureView() with the home instance