【问题标题】:PickerView Can't Change Color Of 2nd RowPickerView 无法更改第二行的颜色
【发布时间】:2018-04-03 14:45:26
【问题描述】:

我在文本字段输入视图中使用选择器视图。它工作得很好,但是当我选择第二行时,它会改变颜色,然后我关闭选择器,当我打开第二次选择器时,所选行的颜色没有改变,但它不仅适用于第二行。其他行工作得很好.

这是我的代码

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {    
var selectedRow = String()
selectedRow = capicityArray[row]
let pickerLabel = UILabel()

if pickerView.selectedRow(inComponent: component) == row {
            pickerLabel.attributedText = NSAttributedString(string: selectedRow, attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 26), NSAttributedStringKey.foregroundColor: GlobalColor.RED_THEME])

        } else {
            pickerLabel.attributedText = NSAttributedString(string: selectedRow, attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 22), NSAttributedStringKey.foregroundColor: GlobalColor.Color153])
        }
        pickerLabel.isOpaque = true
        pickerLabel.textAlignment = .center
        return pickerLabel
    }

输出:

当第一次选择第二行时

然后关闭选择器并再次打开选择器 第二行不会变红

【问题讨论】:

  • 这是发生的,因为每次您都在创建新的标签对象
  • @RahulGUsai 当我声明时间选择器视图不显示任何行的标签的全局变量时
  • 你希望你选择的行是红色的,只有选择的行吗?
  • @ReinierMelian 是的

标签: ios swift uipickerview


【解决方案1】:

使用UIPickerViewDelegatedidSelectRow 方法并在此处更改字体颜色和字体大小

更新

我已将此代码添加到选择中,以避免代码重复

func applySelection(pickerView : UIPickerView, row:Int, component:Int) {
        if let selectedView = pickerView.view(forRow: row, forComponent: component)  as? UILabel {
            selectedView.attributedText = NSAttributedString(string: (selectedView.attributedText?.string)!, attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 26), NSAttributedStringKey.foregroundColor: GlobalColor.RED_THEME])
        }
    }

修复预选问题

您可以在viewDidAppear 或显示您的pickerView 时简单地添加此代码

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.applySelection(pickerView: self.pickerView, row: 0, component: 0)
}

那你就可以这样使用了

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

        let pickerLabel = UILabel()
        pickerLabel.attributedText = NSAttributedString(string: capicityArray[row], attributes: attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 22), NSAttributedStringKey.foregroundColor: GlobalColor.Color153])
        pickerLabel.isOpaque = true
        pickerLabel.textAlignment = .center
        return pickerLabel
    }

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

【讨论】:

  • 当我们选择行时它的代码工作但是预选的行呢。
  • @darshdixit 我的回答最终对你有帮助吗?
  • 不,因为在选择器视图中第一次单击时,我没有说是预选的。我说当我们在选择器视图中第二次单击时预选,时间选择器视图应该显示上次选择行的选定行。 @ReinierMelian
  • 好吧对不起,我不太明白,你只需要预选行中的颜色吗? @darshdixit
猜你喜欢
  • 1970-01-01
  • 2014-04-13
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 2014-12-29
相关资源
最近更新 更多