[英]Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource'
I am following a tutorial where using the UIPickerController
to operate the camera. 我正在学习使用
UIPickerController
操作相机的教程。 However when implementing UICollectionViewDatsaSource
, I get an error saying that ViewController
does not conform to the UICollectionViewDataSource
protocol. 但是,在实现
UICollectionViewDatsaSource
,我收到一条错误消息,指出ViewController
不符合UICollectionViewDataSource
协议。
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate
Any idea on how to fix this problem? 有关如何解决这个问题的任何想法?
You must implement this two method in your ViewController
class: 您必须在
ViewController
类中实现这两个方法:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}
PS - Your function prototype should exactly match with above functions.(remove any '!'
if present) PS - 您的函数原型应与上述函数完全匹配。(如果存在,删除任何
'!'
)
You have to implement these two method in your ViewController class for Collection View : 您必须在ViewController类中为Collection View实现这两个方法:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
<#code#>
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
<#code#>
}
Adding the protocol definition to your custom class is not enough. 将协议定义添加到自定义类是不够的。 You have to provide the required functions of the protocol.
您必须提供协议所需的功能。 See the documenation of the protocol, you have to implement at least:
查看协议的文档,您必须至少实现:
collectionView:numberOfItemsInSection:
collectionView:cellForItemAtIndexPath:
The UICollectionViewDataSource has two functions must be implemented!(they are required functions of the protocol). UICollectionViewDataSource必须实现两个函数!(它们是协议所需的函数)。
To fix the problem,just implement the two functions like this: enter image description here 要解决此问题,只需实现以下两个功能: 在此处输入图像描述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.