2016-11-15 40 views
0

錯誤:錯誤:類型 'GearViewController' 不符合協議 'UIPickerViewDataSource'

Type 'GearViewController' does not conform to protocol 'UIPickerViewDataSource'

基於apple documentation只有2個用於UIPickerViewDataSource所需的方法。兩者都包含在下面的代碼中。我認爲語法是正確的。 (但可能不是)

類/控件聲明和init。 (很多其他的代碼爲清楚起見去除了全部代碼中如果需要的話,我將修改,只是試圖保持簡單。)

class GearViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate{ 

@IBOutlet weak var pickerGearCategory: UIPickerView! 

override func viewDidLoad() { 

     super.viewDidLoad() 

     pickerGearCategory.dataSource = self 
     pickerGearCategory.delegate = self 

    } 

委託和數據源

let gearCategoryPickerData = CategoryRepository.allCategories() 
    //MARK: CategoryPicker- Delegates and data sources 
    //MARK: CategoryPicker - Data Sources 


    //Required 
    func numberOfComponents(in pickerGearCategory: UIPickerView) -> Int { 
     return 1 
    } 

    //Required 
    func pickerGearCategory(pickerGearCategory: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return gearCategoryPickerData.count 
    } 


    func pickerGearCategory(pickerGearCategory: UIPickerView,titleForRow row: Int, forComponent component: Int) -> String? { 
     return gearCategoryPickerData[row].name 
    } 

回答

0

一個你已經實現了協議的方法是錯誤的,該方法應該是:

func pickerView(UIPickerView, numberOfRowsInComponent: Int) { 
//Your implementation here 
} 

你已經實現了方法,

func pickerGearCategory(pickerGearCategory: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
//Your implementation 
} 

糾正,你應該很好去。

+0

有語法錯誤,這個和下面兩個答案。想要'func pickerView(_:UIPickerView,numberOfRowsInComponent組件:Int) - > Int { return gearsCategoryPickerData.count }' –

+0

符合變化,但需要驗證獲得預期結果,並且必須向真正的工作出發。 –

+0

謝謝,這一改變就像一個魅力。更好的是,它指出了下一個帖子的另一個問題! –

0

numberOfComponents是存在的,但在那裏pickerView?

0

只需添加func pickerView(UIPickerView, numberOfRowsInComponent: Int) 功能

相關問題