![](/img/trans.png)
[英]ios 8.1: Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource'
[英]Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource'
我正在學習使用UIPickerController
操作相機的教程。 但是,在實現UICollectionViewDatsaSource
,我收到一條錯誤消息,指出ViewController
不符合UICollectionViewDataSource
協議。
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate
有關如何解決這個問題的任何想法?
您必須在ViewController
類中實現這兩個方法:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}
PS - 您的函數原型應與上述函數完全匹配。(如果存在,刪除任何'!'
)
您必須在ViewController類中為Collection View實現這兩個方法:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
<#code#>
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
<#code#>
}
將協議定義添加到自定義類是不夠的。 您必須提供協議所需的功能。 查看協議的文檔,您必須至少實現:
collectionView:numberOfItemsInSection:
collectionView:cellForItemAtIndexPath:
UICollectionViewDataSource必須實現兩個函數!(它們是協議所需的函數)。
要解決此問題,只需實現以下兩個功能: 在此處輸入圖像描述
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.