在选择器视图Swift 1.2中更改所选行标签的颜色 [英] Change selected row label color in picker view swift 1.2

查看:38
本文介绍了在选择器视图Swift 1.2中更改所选行标签的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下代码创建了pickerview

I have a created the pickerview with the following code

    @IBOutlet var myPicker: UIPickerView!

    var colors: [String] = ["red","green","blue"]

    override func viewDidLoad() {
            super.viewDidLoad()     

            myPicker = UIPickerView()
            myPicker.dataSource = self
            myPicker.delegate = self
    }

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

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

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
            return colors[row] as! String
        }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        }

现在,我想更改所选行的标签(文本)颜色,例如,如果我向下滚动并选择蓝色,则文本颜色应更改为橙色,而其他2个标签将为黑色,当我选择时也是如此其他行.我已经尝试了以下代码,但是没有用

Now i want to change the label (text) color of the selected row, for example if i scroll down and select blue, the text color should change to orange and the other 2 labels will be black, same follows when i select other rows. i have tried the following code but it doesn't work

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
        var pickerLabel = UILabel()
        var myTitle:NSAttributedString
        let titleData = colors[row]
        if pickerView.selectedRowInComponent(component) == row {
            myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName: UIColor.redColor()])
        } else {
            myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName: UIColor.blueColor()])
        }

        //This way you can set text for your label.
        pickerLabel.attributedText = myTitle

        return pickerLabel

    }

我不知道应该使用didSelectRow还是viewForRow方法来实现它,有人可以帮我吗?

I dont know whether i should implement it with didSelectRow or viewForRow method, can someone please help me on this?

推荐答案

您可以执行以下操作:

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

  var colors = ["red","green","blue"]

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

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

  func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let color = (row == pickerView.selectedRowInComponent(component)) ? UIColor.orangeColor() : UIColor.blackColor()
    return NSAttributedString(string: colors[row], attributes: [NSForegroundColorAttributeName: color])
  }

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

这篇关于在选择器视图Swift 1.2中更改所选行标签的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆