我一直在寻找答案,但我找不到任何明确的解决方案。我正在使用具有相同UIpickerViews的三个UItextField创建一个视图控制器。如果已在另一个UItextField中选择了特定行,则如何使特定行无法选择更改颜色或消失?
这是我的代码:
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Array.count
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
if "Shake" {
let attributesUnavailable = [NSForegroundColorAttributeName: UIColor.gray]
let attributedTitle = NSAttributedString(string: "Shake", attributes: attributesUnavailable)
return attributedTitle
}
let attributesAvailable = [NSForegroundColorAttributeName: UIColor.black]
let attributedTitle = NSAttributedString(string: Array[row], attributes: attributesAvailable)
return attributedTitle
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func donePicker() {
actionTextField1.resignFirstResponder()
actionTextField2.resignFirstResponder()
actionTextField3.resignFirstResponder()
}
@IBAction func save3(_ sender: AnyObject) {
if (behaviorTextField1.text?.isEmpty)! {
let behavior1Empty = UIAlertController (title: "Error", message: "Please fill-In Behavior Names and set corresponding Actions", preferredStyle: UIAlertControllerStyle.alert)
self.present(behavior1Empty, animated:true, completion:nil)
behavior1Empty.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction!) in
}))
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if Array[row] {
pickerView.selectRow(row + 1, inComponent: 0, animated: true)
}
}
if pickerView.tag == 1 {
actionTextField1.text = Array[row]
}
else if pickerView.tag == 2 {
actionTextField2.text = Array[row]
}
else {
actionTextField3.text = Array[row]
} 帮助将受到高度赞赏!
答案 0 :(得分:0)
它不像UITableView
那么简单,但有一个很好的方法,在Settings.app设置IOS日期时似乎使用了相同的方法。
示例:
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
if row_cant_be_selected {
let attributesUnavailable = [NSForegroundColorAttributeName: UIColor.grayColor()]
let attributedTitle = NSAttributedString(string: YOUR_TITLE_FOR_ROW, attributes: attributesUnavailable)
return attributedTitle
}
let attributesAvailable = [NSForegroundColorAttributeName: UIColor.blackColor()]
let attributedTitle = NSAttributedString(string: YOUR_TITLE_FOR_ROW, attributes: attributesUnavailable)
return attributedTitle
}
selectRow(_:inComponent:animated:)
调用为"强制"另一行选择。示例:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if ROW_CANT_BE_SELECTED {
pickerView.selectRow(row + 1, inComponent: 0, animated: true)
}
}
IE:如果第1行不可用,请在pickerView(_:didSelectRow:inComponent:
上拨打pickerView.selectRow(row + 1, inComponent: 0, animated: true)
。这将使UIPickerView
跳到下一个元素。请注意最后一个元素。
请注意row + 1
,如果当前row
是最后一个,则应该row - 1
。
其他方法是删除数据源中的那些不可用元素。只需重新加载其他选择器视图数据。