2014-09-26 106 views
13

我正在學習使用UIPickerController來操作攝像頭的教程。但是,在執行UICollectionViewDatsaSource時,出現錯誤,說明ViewController不符合UICollectionViewDataSource協議。類型'ViewController'不符合協議'UICollectionViewDataSource'

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

有關如何解決此問題的任何想法?

回答

2

將協議定義添加到您的自定義類是不夠的。你必須提供協議所需的功能。見協議的documenation,你必須至少實現:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {} 

附註:

collectionView:numberOfItemsInSection: 
collectionView:cellForItemAtIndexPath: 
25

您必須在ViewController類實現這個兩分法 - 您的函數原型應該與上述函數完全匹配(刪除任何'!',如果存在的話)

+1

對我來說是在cellForItemAtIndexPath函數中的每個參數之後 – Chris 2014-10-21 11:32:05

+0

對於不執行所需的協議方法,默認行爲似乎是一個紅色錯誤,而在Objective-C中,Xcode只是給出了一個黃色警告。您必須至少刪除所需的方法才能使錯誤消失。 – 2015-11-16 02:14:50

-1

UICollectionViewDataSource有兩個功能必須實現!(它們是協議的必需函數)。

要解決這個問題,只需要實現這樣的兩個功能:「!」 enter image description here

相關問題