错误:
类型“ GearViewController”不符合协议
'UIPickerViewDataSource'
基于apple documentation,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
}
最佳答案
您实现的一种协议方法是错误的,该方法应为:
func pickerView(UIPickerView, numberOfRowsInComponent: Int) {
//Your implementation here
}
您已将方法实现为
func pickerGearCategory(pickerGearCategory: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//Your implementation
}
改正它,您应该就很好了。
关于ios - 错误:类型“GearViewController”不符合协议(protocol)“UIPickerViewDataSource”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40611758/