UIPickerView没有注册正确的最后一行。 Swift 2.0

时间:2016-03-08 15:06:34

标签: ios swift uipickerview uipicker

我有一个2个UIPickerView,一个叫做dayPicker,你可以选择1-12和durationPicker,你可以选择天,周,月和年。

以下代码如下:

class AddNewViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
    @IBOutlet weak var dayPicker: UIPickerView!
    @IBOutlet weak var durationPicker: UIPickerView!
    var durationDay: Double?
    var durationType: String?

    var durationPickerDataSource = ["Day(s)","Week(s)","Month(s)","Year(s)"];
     var dayPickerDataSource = ["1","2","3","4","5","6","7","8","9","10","11","12"];


func numberOfComponentsInPickerView(dayPickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == durationPicker {
        return durationPickerDataSource.count;
    }
    else {
        return dayPickerDataSource.count;
    }
}


func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == durationPicker{
        durationType = durationPickerDataSource[row]
        print("")
        print("")
        print("\(row): type == \(durationType)")
        print("\(row)")
        print("")
        print("")

        return durationType
    }
    else {
        durationDay = Double(dayPickerDataSource[row])
        return dayPickerDataSource[row]
    }
}
 override func viewDidLoad() {
        super.viewDidLoad()

        self.durationPicker.dataSource = self;
        self.durationPicker.delegate = self;
        self.dayPicker.dataSource = self;
        self.dayPicker.delegate = self;
    }
}

当我在模拟器中选择行时,它会正确打印所有行,而不是打印出的最后一行"周("。

我对拣货员有以下限制:

dayPicker:

daypicker

durationPicker:

durationPicker

1 个答案:

答案 0 :(得分:1)

复制你的代码,一切似乎都运行正常。我唯一的猜测是你正在修改你的数据源,因此选择器委托返回错误的信息。

我测试了你的代码(没有修改),选择器显示正确的值......

编辑:

从评论部分看来问题似乎在于没有指定UIPickerView的{​​{1}}方法,添加它似乎解决了OP的问题(虽然不能详细说明“为什么”)。 ..):

didSelectRow