【问题标题】:Making a row (in a pickerview) not selectable after selected in a UItextField with same pickerview在具有相同选择器视图的 UItextField 中选择后,使行(在选择器视图中)不可选择
【发布时间】:2016-11-19 14:39:09
【问题描述】:

我一直在寻找答案,但找不到任何明确的解决方案。我正在制作一个具有相同 UIpickerViews 的三个 UItextFields 的视图控制器。如果特定行已经在另一个 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]

} 非常感谢您的帮助!

【问题讨论】:

    标签: swift uipickerview


    【解决方案1】:

    它不像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
    }
    
    • 然后使用委托的方法pickerView(_:didSelectRow:inComponent:) 知道用户何时选择了不可用的行。在此方法中,只需调用 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

    其他方法是删除数据源中那些不可用的元素。只需重新加载其他选择器视图数据。

    【讨论】:

    • 感谢@Caio 的回复!!!我真的是编程新手。我应该在哪里写 pickerView(:attributedTitleForRow:forComponent:) 并设置为灰色?和 pickerView(:didSelectRow:inComponent:) ?谢谢!!!
    • pickerView(:attributedTitleForRow:forComponent:) 是您已经在使用的UIPickerViewDelegate 的一部分(titleForRowdidSelectRow 已经是委托的函数)。 pickerView(:didSelectRow:inComponent:) 它已经在您展示的代码中。试试看,如果不行,我可以给你写一些例子。
    • 是的。这个: } func pickerView(_ pickerView: UIPickerView, attributesTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 如何将其设置为灰色?
    • 好的,我添加了一些示例。希望能帮助到你。但它在 swift 2.2 中。它与 Swift 3 没有太大区别(我现在没有 XCode 8)。
    • 谢谢Caio!我应该为 "row_cant_be_selected" 和 String: "Your Title for row" 写什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多