一个View Controller上的两个CollectionView

时间:2018-05-19 20:53:57

标签: ios swift uicollectionview

我试图在一个视图控制器上有两个集合视图。有很多帖子引导人们通过这个,但我不知道我做错了什么。

我的代码如下,

class FeedVC: UIViewController {
    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet var collectionViewProgress: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.dataSource = self
        collectionView.delegate = self
        collectionViewProgress.dataSource = self
        collectionViewProgress.delegate = self
    }
}

extension FeedVC:  UICollectionViewDelegate, UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (collectionView == self.collectionView){
            return 10
        }else{
            return 1
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print("hre")
        if (collectionView == self.collectionView){
            if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UnclaimedCVC", for: indexPath) as? UnclaimedCVC {
                cell.configureCell()
                return cell
            }else{
                return UICollectionViewCell()
            }
        }else if(collectionView == self.collectionViewProgress){
            if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InProgressCVC", for: indexPath) as? InProgressCVC {
                cell.configureCell()
                return cell
            }else{
                return UICollectionViewCell()
            }
        }else{
            return UICollectionViewCell()
        }
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        if (collectionView == self.collectionView){
            return 1
        }else{
            return 1
        }

    }
}

我目前收到错误,

  

'无法将类视图出列:UICollectionElementKindCell with   标识符InProgressCVC - 必须注册一个nib或类   标识符或连接故事板中的原型单元'

1 个答案:

答案 0 :(得分:0)

viewDidLoad put

collectionView.register(UnclaimedCVC.self, forCellWithReuseIdentifier: "UnclaimedCVC ")

collectionViewProgress.register(InProgressCVC.self, forCellWithReuseIdentifier: "InProgressCVC ")

如果您将它们创建为Xibs

 collectionView.register(UINib.init(nibName: "UnclaimedCVC", bundle: nil), forCellWithReuseIdentifier: "UnclaimedCVC")

 collectionViewProgress.register(UINib.init(nibName: "InProgressCVC", bundle: nil), forCellWithReuseIdentifier: "InProgressCVC")