如何在swift 4中删除和添加uipickerview中的项目?

问题描述 投票:2回答:1

我有三个文本字段,我显示相同的选择器视图和下拉选择值。所以条件是我在数组中有5个值,其值为红色,蓝色,绿色,黄色,黑色。所以条件是

  1. 这三个文本字段不能具有相同的值。也就是说,如果对于第一个文本字段1,如果我选择红色作为选择器的值,则当我选择文本字段2或文本字段3时,应该从选择器视图中删除或禁用值“红色”。
  2. 如果我从选择器视图将textfield 1的值从红色更改为黑色,则当我单击textfield 2或textfield 3时,禁用或删除的值red应该被添加回pickerview。

我正在尝试的代码是:

import UIKit


class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var textFiled1: UITextField!
    @IBOutlet weak var textFiled2: UITextField!
    @IBOutlet weak var textFiled3: UITextField!
    @IBOutlet weak var pickerView: UIPickerView!

    var Array = ["Blue", "Green", "Red", "White", "Grey"]
    var indexOfPicker = Int()

    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView.dataSource = self
        pickerView.delegate = self
    }

    @IBAction func minusButton(_ sender: UIButton) {
        if Array.count != 0 {
            Array.remove(at: indexOfPicker)
            pickerView.reloadAllComponents()
        }
    }

    @IBAction func plusButton(_ sender: UIButton) {
        if textFiled.text != "" {
            Array.append(textFiled.text!)
            pickerView.reloadAllComponents()
        }
    }

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

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

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return Array[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        indexOfPicker = row
    }    
}
ios iphone xcode swift4 uipickerview
1个回答
2
投票
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

    @IBOutlet weak var textFiled1: UITextField!
    @IBOutlet weak var textFiled2: UITextField!
    @IBOutlet weak var textFiled3: UITextField!
    @IBOutlet weak var pickerView: UIPickerView!

    var selectedTextField:UITextField?

    var colorsArray = ["Blue", "Green", "Red", "White", "Grey"]

    override func viewDidLoad() {
        super.viewDidLoad()

        textFiled1.delegate = self
        textFiled2.delegate = self
        textFiled3.delegate = self

        pickerView.dataSource = self
        pickerView.delegate = self
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        let tempArr = colorsArray.filter { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
        return tempArr.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let tempArr = colorsArray.filter { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
        return tempArr[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let tempArr = colorsArray.filter { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
        self.selectedTextField?.text = tempArr[row]
        pickerView.reloadAllComponents()
    }
    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.selectedTextField = textField
        pickerView.reloadAllComponents() 
    }
}

推荐问答