首页 > 解决方案 > UIPickerView 选定的行标题设计在行值等于 1 时未更改

问题描述

我有 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()

谢谢您的帮助。祝你今天过得愉快。

标签: iosswiftswift4uipickerviewuipickerviewdelegate

解决方案


推荐阅读