我正在尝试将 Collection View 添加到名为“cardsViewController”的自定义 View Controller 中。当我运行构建时,出现错误 [UIViewController collectionView:numberOfItemsInSection:]: 无法识别的选择器发送到实例
我已将 Collection View 的数据源和委托(delegate)连接到 Storyboard中的 View Controller ,并在代码中的类名后面添加了“UICollectionViewDataSource、UICollectionViewDelegate”。我还通过将“CardsViewController”(与代码文件同名)添加到 View Controller 的自定义类字段,将 Storyboard中的 View Controller 连接到代码。
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 能够识别 View Controller 代码文件负责控制 Collection View 。
最佳答案
当您在界面构建器中设置错误的 View Controller 时,通常会发生这种情况。确保 Identity Inspector
中的Custom Class
与您的 View Controller 类名称 (CardsViewController
) 完全匹配。
关于swift - 无法将 Collection View 连接到 View Controller 代码。 'Error unrecognized selector sent to instance',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583807/