PickerView未显示选择分隔符 - iOS

时间:2018-03-07 07:06:45

标签: ios swift uipickerview

我使用UIPickerView进行性别选择。问题是,它没有显示选择分隔符 这是我的代码:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return pickerDataArr != nil ? 1 : 0
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerDataArr!.count
}

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

    if label == nil {
        label = UILabel()
    }

    label!.font = UIFont.systemFont(ofSize: 15)
    label!.textAlignment = .center
    label!.text = pickerDataArr![row]
    return label!
}

输出 -

enter image description here

我尝试了这些选项:
1默认选择第一行:

pickerView.selectRow(0, inComponent: 0, animated: false)

2设置属性:

pickerView.showsSelectionIndicator = true

仍然没有运气......
如果有人有任何想法,请分享!

2 个答案:

答案 0 :(得分:0)

在尝试了更多选项之后,我有了选择指标。

这是改变:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerDataArr?.count ?? 0
}

一切都和原来一样。不知何故,它与numberOfComponents的选择器视图有关。

甚至不需要:

pickerView.selectRow(0, inComponent: 0, animated: false)
pickerView.showsSelectionIndicator = true

<强>输出 -
enter image description here

答案 1 :(得分:0)

enter image description here 以下是更改

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return pickerDattaArr.count != 0 ? 1 : 0

}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerDattaArr.count
}

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

    var label = view as? UILabel
    if label == nil {
        label = UILabel()
    }
    label!.font = UIFont.systemFont(ofSize: 15)
    label!.textAlignment = .center
    label!.text = pickerDattaArr[row]
    return label!
}