在UIPickerView Swift中更改所选值的颜色

时间:2016-01-21 14:13:19

标签: ios swift uipickerview

我有UIPickerView使用名称Array

显示名称

数组的第一个元素是默认值"选择名称"

我希望以Gary颜色显示所选值,其余名称以蓝色显示

此func用于防止选择默认值:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if row == 0 {
        pickerView.selectRow(row+1, inComponent: component, animated: true)
        selectedName = names[row+1]
    } else {
        selectedName = names[row]
    }
}

和这个改变颜色的函数

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
    let pickerLabel = UILabel()
    pickerLabel.textColor = (row == pickerView.selectedRowInComponent(component)) ? UIColor.grayColor() : UIColor.blueColor()
    pickerLabel.text = names[row].name
    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
}

问题是当我选择拾取器的第一个值(不是默认值)时,颜色保持蓝色。所有其他项目都正常工作(我选择它们时颜色会变为灰色)

3 个答案:

答案 0 :(得分:2)

Use the PickerView reloadComponentMethod

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if row == 0 {
            pickerView.selectRow(row+1, inComponent: component, animated: true)
            pickerView.reloadComponent(component)
            selectedName = names[row+1]
        }
        else {
            selectedName = names[row]
        }
    }

selectRow仅将滚轮旋转到新行。调用reloadComponent刷新数据源以获取新视图

答案 1 :(得分:1)

var calendarContents : [[String]] = []

let dates = ["1","2","3","4","5","6"]
let months = ["JAN", "FEB", "MAR"]
let year = ["2015", "2016","2017"]

override func viewDidLoad() {
    super.viewDidLoad()
    calendarContents = [dates,months,year]

    // Do any additional setup after loading the view.
}


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


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

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

    let pickerLabel = UILabel()

    if pickerView.selectedRow(inComponent: component) == row {
        pickerLabel.attributedText = NSAttributedString(string: calendarContents[component][row], attributes: [NSFontAttributeName:UIFont(name: "AvenirNext-Bold", size: 26.0)!, NSForegroundColorAttributeName: UIColor.yellow])

    } else {
        pickerLabel.attributedText = NSAttributedString(string: calendarContents[component][row], attributes: [NSFontAttributeName:UIFont(name: "Avenir Next", size: 22.0)!, NSForegroundColorAttributeName: UIColor.white])
    }

    pickerLabel.textAlignment = .center
    return pickerLabel
}


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    pickerView.reloadAllComponents()
}

答案 2 :(得分:0)

我只是在返回pickerView viewForRowpickerView attributedTitleForRow上的值之前执行此操作:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  pickerView.reloadAllComponents() // hack to refresh all values while scrolling
})

它很笨拙,但对我来说效果很好。希望对您有所帮助:)