使用从第一列中选​​取的内容从JSON在UIPickerView中显示第二列

时间:2019-04-25 05:11:05

标签: ios swift uipickerview

我有一个具有以下结构的JSON脚本:

[{"manufacturer”:”exampleManufacturer”,”model":"exampleModel”}]

我目前具有以下UIPickerView设置,显示第一列manufacturer如何在同一UIPickerView中显示第二列model

第二列取决于第一列选择的内容,因此必须重新加载。

以下是我到目前为止显示第一列manufacturer的内容:

import UIKit

class CreateRMA_ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {



        @IBOutlet weak var customerTextField: UITextField!


    var pickerView: UIPickerView!
    var values: [AnyObject] = []

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return self.values.count

    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {


        let titleRow = (values[row] as? String)!
        return titleRow


    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {


        if values.count > 0 && values.count >= row{
            self.customerTextField.text = self.values[row] as? String
            self.pickerView.isHidden = true
        }

    }




    func textFieldDidBeginEditing(_ textField: UITextField) {
        //when you select the text field the picker view will be visible

        if(textField == self.customerTextField){
            self.pickerView.isHidden = false
            self.view.endEditing(true)
        }

    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


        override func viewDidLoad() {
            super.viewDidLoad()

            pickerView = UIPickerView()
            pickerView.dataSource = self
            pickerView.delegate = self


            //get the values from sql/Json
            let url = NSURL(string: "https://www.example.com/test/test.php")

            let data = NSData(contentsOf: url! as URL)
            var tmpValues = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
            tmpValues = tmpValues.reversed() as NSArray
            reloadInputViews()


            for candidate in tmpValues {
                if let cdict = candidate as? NSDictionary {

                    //fullName is the column name in sql/json
                    let names = cdict["customer"]
                    self.values.append(names! as AnyObject)


                }
            }
    }

}

JSON示例:

[{"manufacturer":"GE","model":"1147"},{"manufacturer":"GE","model":"34983"},{"manufacturer":"Apple","model":"8Plus"},{"manufacturer":"Samsung","model":"Galaxy 8"},{"manufacturer":"Samsung","model":"Note 5"},{"manufacturer":"Samsung","model":"Note 8"}]

2 个答案:

答案 0 :(得分:1)

function name

修改

class TestViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    struct Category {
        var name: String
        var items: [String]
    }
    var allCategories = [Category]()
    var selectedCategory:Category?
    var selectedItem: String?

    var pickerView: UIPickerView!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        allCategories = [Category(name: "Fruits", items: ["Apple","Banana"]), Category(name: "Vegetables", items: ["Cucumber","Potato"])]
        pickerView = UIPickerView(frame: CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 200))
        view.addSubview(pickerView)
        pickerView.dataSource = self
        pickerView.delegate = self


    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return selectedCategory == nil ? 1 : 2
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return component == 0 ? allCategories.count : selectedCategory?.items.count ?? 0
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return allCategories[row].name
        } else {
            return selectedCategory?.items[row]
        }
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            selectedCategory = allCategories[row]
            manufacturerTextField.text = allCategories[row].name
            pickerView.reloadAllComponents()
        } else {
            selectedItem = selectedCategory?.items[row]
            modelTextField.text = selectedCategory?.items[row]
        }
    }

}

enter image description here

答案 1 :(得分:0)

对于pikcerview中的2列,请在numberOfComponentsInPickerView中使用2。

您可以按照此答案。

How do I setup a second component with a UIPickerView