错误:
类型“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
}