[英]iOS - Error Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'
[英]Error: Type 'GearViewController' does not conform to protocol 'UIPickerViewDataSource'
錯誤:
類型“ GearViewController”不符合協議“ UIPickerViewDataSource”
根據Apple文檔 ,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
}
這里有numberOfComponents,但是pickerView在哪里?
您實現的一種協議方法是錯誤的,該方法應為:
func pickerView(UIPickerView, numberOfRowsInComponent: Int) {
//Your implementation here
}
您已經實現了該方法,
func pickerGearCategory(pickerGearCategory: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//Your implementation
}
改正它,您應該就很好了。
只需添加func pickerView(UIPickerView, numberOfRowsInComponent: Int)
函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.