UICollectionView没有链接到我的数据源swift

问题描述 投票:-2回答:1

所以我有2个控制器。登录页面名为viewController,仪表板名为SecondViewController。现在,您单击登录页面并转到第二个视图控制器,在那里创建一个CollectionView

self.performSegue(withIdentifier: "loginSuccessful", sender:  self)

这是我去SecondViewController的话题。所以现在对于SecondViewController,我创建了一个UICollectionView,我在storyboard上创建了自定义单元格,并将CollectionView连接到数据源。现在,当我登录我的SecondViewController时,它会显示以下错误。

SecondViewController collectionView:numberOfItemsInSection:]: unrecognized selector

这是我的SecondViewController代码:

import UIKit


class SecondViewController: UIViewController {

    @IBOutlet weak var UiToolbar: UINavigationItem!

    @IBOutlet var collectionView: UICollectionView!


    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func viewDidAppear(_ animated: Bool) {

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath)

        return cell
    }


}

PhotoCell是我的collectionViewCells的标识符。我感谢任何能帮助我的人。

swift debugging uicollectionview uistoryboardsegue
1个回答
0
投票

我认为你做了一个简单的错误只是将扩展更改为Second View Controller。喜欢

extension SecondViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath)

        return cell
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.