无法将集合视图连接到View Controller Code。 '错误无法识别的选择器发送到实例'

问题描述 投票:0回答:1

我正在尝试将集合视图添加到我的自定义视图控制器“cardsViewController”。当我运行构建时,我收到错误[UIViewController collectionView:numberOfItemsInSection:]:发送到实例的无法识别的选择器

我已将集合视图的数据源和委托连接到故事板中的视图控制器,并在代码中的类名后添加了“UICollectionViewDataSource,UICollectionViewDelegate”。我还通过将“CardsViewController”(与代码文件同名)添加到视图控制器的自定义类字段,将故事板中的视图控制器连接到代码。

import UIKit

class CardsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

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

        return cell
    }

}

我希望xcode能够识别视图控制器代码文件负责控制集合视图。

identity inspector of Cards View Controller

connections inspector of the collection view

swift xcode viewcontroller
1个回答
0
投票

在界面构建器中设置错误的视图控制器时,通常会发生这种情况确保Custom Class中的Identity Inspector与您的视图控制器类名称(CardsViewController)完全匹配。

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