【发布时间】: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