0

错误:

类型“GearViewController”不符合协议“UIPickerViewDataSource”

根据苹果文档,UIPickerViewDataSource 只需要 2 个方法。两者都包含在下面的代码中。我认为语法是正确的。(但可能不是)

类/控件声明和初始化。(为清楚起见,删除了许多其他代码。如果需要,可以使用完整的代码,我会编辑。只是尽量保持简短。)

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
    }
4

3 回答 3

0

numberOfComponents 在那里,但是pickerView 在哪里?

于 2016-11-15T14:06:47.903 回答
0

您实现的协议方法之一是错误的,该方法应该是:

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

您已将方法实现为,

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

纠正它,你应该很高兴。

于 2016-11-15T14:07:07.953 回答
0

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

于 2016-11-15T14:07:28.400 回答