[英]Error: Type 'GearViewController' does not conform to protocol 'UIPickerViewDataSource'
Error: 错误:
Type 'GearViewController' does not conform to protocol 'UIPickerViewDataSource' 类型“ GearViewController”不符合协议“ UIPickerViewDataSource”
Based on apple documentation there are only 2 required methods for a UIPickerViewDataSource. 根据Apple文档 ,UIPickerViewDataSource仅需要2种方法。 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)
函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.