简体   繁体   English

类型'ViewController'不符合协议'UICollectionViewDataSource'

[英]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.

相关问题 ios 8.1:类型“ViewController”不符合协议“UICollectionViewDataSource” - ios 8.1: Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource' 类型“自定义TableViewController”不符合协议“ UICollectionViewDataSource” - Type 'Custom TableViewController' does not conform to protocol 'UICollectionViewDataSource' 类型“ ViewController”不符合协议 - Type 'ViewController' does not conform to protocol Swift:类型&#39;ViewController&#39;不符合协议&#39;UIPageViewControllerDataSource&#39; - Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource' 类型“ viewController”不符合协议“ PathMenuDelegate” - type “viewController” does not conform protocol “PathMenuDelegate” 类型的viewcontroller不符合协议uitableviewdatasource - type viewcontroller does not conform to protocol uitableviewdatasource 类型“ ViewController”不符合协议“ FBSDKLoginButtonDelegate” - The type 'ViewController' does not conform to protocol 'FBSDKLoginButtonDelegate' ViewController类型不符合协议SKPaymentTransactiionObserver - type ViewController does not conform to protocol SKPaymentTransactiionObserver 类型“ ViewController”不符合协议“ UITableViewDataSource” - Type “ViewController” does not conform to protocol 'UITableViewDataSource" 类型“ViewController”不符合协议“UITableViewDataSource” - Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM