UIPickerView出错

时间:2016-10-31 07:28:01

标签: swift uipickerview

我收到这些错误,我甚至不知道为什么

1:类型视图控制器不符合UIViewPickerDataSource

2:方法pickerview(pickerView:numberOfRowsInComponent :)具有不同的参数名称

这是我的代码! 任何帮助将不胜感激。感谢

import UIKit

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

@IBOutlet weak var timePicker: UIPickerView!

var timePickerData:[[Int]] = [[Int]]()

override func viewDidLoad()
{
    super.viewDidLoad()

    self.timePicker.delegate = self
    self.timePicker.dataSource = self

    timePickerData = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,52,  53, 54, 55, 56, 57, 58, 59], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,52,  53, 54, 55, 56, 57, 58, 59]]
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
    return timePickerData.count
}

// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    return timePickerData[component].count
}

// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int?
{
    return timePickerData[component][row]
}

}

1 个答案:

答案 0 :(得分:1)

您似乎正在使用XCode 8,但您的代码使用旧的Swift 2.2 UIPickerView API。我的修正建议:

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

@IBOutlet weak var timePicker: UIPickerView!

var timePickerData:[[Int]] = [[Int]]()

override func viewDidLoad()
{
    super.viewDidLoad()

    self.timePicker.delegate = self
    self.timePicker.dataSource = self

    timePickerData = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,52,  53, 54, 55, 56, 57, 58, 59], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,52,  53, 54, 55, 56, 57, 58, 59]]
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return timePickerData.count
}

// The number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    return timePickerData[component].count
}

// The data to return for the row and component (column) that's being passed in
private func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int?
{
    return timePickerData[component][row]
}

}