简体   繁体   中英

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

Error:

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

Based on apple documentation there are only 2 required methods for a UIPickerViewDataSource. Both are included in the code below. I think the syntax is correct. (but probably not)

Class/control declaration, and init. (lots of other code removed for clarity. Full code available if needed, i'll edit. Just trying to stay brief.)

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

@IBOutlet weak var pickerGearCategory: UIPickerView!

override func viewDidLoad() {

        super.viewDidLoad()

        pickerGearCategory.dataSource = self
        pickerGearCategory.delegate = self

    }

Delegate and datasources

  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
    }

这里有numberOfComponents,但是pickerView在哪里?

One of the protocol method that you've implemented is wrong, the method should be:

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

You've implemented the method as,

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

Correct it and you should be good to go.

只需添加func pickerView(UIPickerView, numberOfRowsInComponent: Int)函数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM