UIPickerView选定的行标题设计不会更改,而行值等于1

时间:2018-09-05 19:27:56

标签: ios swift swift4 uipickerview uipickerviewdelegate

我有2个uipickerviews用于选择小时和分钟值。当我想为他们两个都选择默认值时,如果selectedRow = 1标题颜色变为灰色(这意味着未选中行)并且此问题仅在首次加载时发生(触发viewDidLoad时)。除这些问题外,颜色已按预期更改。

这是我的选择器委托方法,用于更改选择器标签及其规格。更改选择器值时,此功能非常有用。

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    var label = UILabel()
    if let v = view as? UILabel { label = v }
    label.font = UIFont (name: "Muli-Bold", size: 22)

    if pickerView.selectedRow(inComponent: component) == row {
        label.textColor = StyleManager.colors.deviceModeActiveBlue
    } else {
        label.textColor = StyleManager.colors.pickerLightGray
    }

    if (pickerView.tag == 1){
        label.text =   "\(hours[row])"
    }else{
        label.text =  "\(minutes[row])"
    }

    label.textAlignment = .center
    return label
}

在viewDidLoad

pickerHour.selectRow(Int(calculateDelayTime(delayTime: currentDevice.dwProgramDelayDuration!)[0])!, inComponent: 0, animated: true)
pickerHour.reloadAllComponents()
pickerMinute.selectRow(Int(calculateDelayTime(delayTime: currentDevice.dwProgramDelayDuration!)[1])!, inComponent: 0, animated: true)
pickerMinute.reloadAllComponents()

当selectRow值等于1时,行标题颜色为灰色(表示未选择该行),但选择了第一行。如果它的颜色不同于1,则标题颜色为蓝色(应该是蓝色),并且选中的行也会被选中。

换句话说,这两个示例按预期工作。

pickerHour.selectRow(2, inComponent: 0, animated: true)
pickerHour.reloadAllComponents()
pickerMinute.selectRow(2, inComponent: 0, animated: true)
pickerMinute.reloadAllComponents()

pickerHour.selectRow(0, inComponent: 0, animated: true)
pickerHour.reloadAllComponents()
pickerMinute.selectRow(0, inComponent: 0, animated: true)
pickerMinute.reloadAllComponents()

这不是。

pickerHour.selectRow(1, inComponent: 0, animated: true)
pickerHour.reloadAllComponents()
pickerMinute.selectRow(1, inComponent: 0, animated: true)
pickerMinute.reloadAllComponents()

感谢您的帮助。祝你有美好的一天。

0 个答案:

没有答案