swift - 无法将集合视图连接到视图控制器代码。'错误无法识别的选择器发送到实例'
问题描述
我正在尝试向我的自定义视图控制器添加一个名为“cardsViewController”的集合视图。当我运行构建时,我收到错误 [UIViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance
我已将集合视图的数据源和委托连接到情节提要中的视图控制器,并在代码中的类名之后添加了“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 能够识别视图控制器代码文件负责控制集合视图。
解决方案
当您在界面生成器中设置错误的视图控制器时,通常会发生这种情况。确保Custom Class
inIdentity Inspector
与您的视图控制器类名 ( CardsViewController
) 完全匹配。
推荐阅读
- python - 我如何下载 requirements.txt?
- javascript - 如何添加私钥以将智能合约部署到 Roptsen?
- probability - 设计一个实验,从 100 个硬币中找出不公平的硬币
- docbook - 如何为非技术性写作定制 DocBook?
- reactjs - 从 router.query 中正确提取字符串
- java - 在二叉树中插入字符串
- windows - 通过 ssh 向 Windows 服务器传递命令
- wordpress - wordpress 上的图片、链接和按钮不起作用
- file - 如何在 Prolog 中保存和加载数据文件?
- python - 从 Django 中的 views.py 和模板重定向到管理面板(超级用户)登录页面