Swift PickerView不显示数组元素

时间:2016-01-12 20:04:25

标签: arrays json swift uipickerview

我的" picker2" pickerview不会显示来自" modelArray"的数据。阵列。当我复制数组时,我有数据。但是在pickerview中根本没有显示任何数据。

我记住了所有事情并做了我的研究如何去做。但没有成功。

任何帮助都将受到高度赞赏:)



import UIKit

class MakeViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var txt_make: UITextField!
    @IBOutlet weak var txt_model: UITextField!
    
    var makeArray = [String]()
    var modelArray = [String]()
    
    var picker = UIPickerView()
    var picker2 = UIPickerView()
    
    var numberOfRowsMake = 0
    var numberOfRowsModel = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        picker.delegate = self
        picker.dataSource = self
        
        picker2.delegate = self
        picker2.dataSource = self
        
        txt_make.inputView = picker
        txt_model.inputView = picker2
        
        txt_make.clearButtonMode = .Always
        txt_model.clearButtonMode = .Always
        
        parseJSON2()


    }
    
    func parseJSON2() {
                
        let path : String = NSBundle.mainBundle().pathForResource("json2", ofType: "json")! as String
        let jsonDATA = NSData(contentsOfFile: path) as NSData! 
        
        numberOfRowsMake = readableJSON["Cars"].count
        
        for var i = 0; i < numberOfRowsMake; ++i {      
            let make = readableJSON["Cars"][i]["Manufacturer"].stringValue 
            makeArray.append(make)
        }
    }
    
    func parseJSON3() {
                
        let path : String = NSBundle.mainBundle().pathForResource("json2", ofType: "json")! as String
        let jsonDATA = NSData(contentsOfFile: path) as NSData! 
        
        numberOfRowsMake = readableJSON["Cars"].count
        
        for var i = 0; i < numberOfRowsMake; ++i {     
            let make = readableJSON["Cars"][i]["Manufacturer"].stringValue 
            let model = readableJSON["Cars"][i]["Model"].stringValue 
            
            print(txt_make.text)
            
            if make == txt_make.text {
                modelArray.append(model)                   
            }
        }            
        dump(modelArray)    
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        if pickerView == picker {
            return numberOfRowsMake
        }
        if pickerView == picker2 {       
            return numberOfRowsModel
        }   
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        if pickerView == picker {                           
            txt_make.text = makeArray[row]               
        }        
        if pickerView == picker2 {               
            self.picker2.reloadAllComponents()               
            parseJSON3()                
            txt_model.text = modelArray[row]
        }
        
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        if pickerView == picker {
                return makeArray[row]
        }
        if pickerView == picker2 {
            
                return modelArray[row]
        }            
        return ""
    }
}
&#13;
&#13;
&#13;

这是本地JSON文件。

&#13;
&#13;
{
    "Cars" :
[{
  "Manufacturer": "ABARTH1",
  "Model": "500, 2012 onwards",
  }, {
  "Manufacturer": "ABARTH1",
  "Model": "500, 2012 onwards",
  }, {
  "Manufacturer": "ABARTH1",
  "Model": "500, 2012 onwards",
  }, {
  "Manufacturer": "ABARTH2",
  "Model": "500, 2012 onwards",
  }, {
  "Manufacturer": "ABARTH2",
  "Model": "500, 2012 onwards",
  }, {
  "Manufacturer": "ABARTH2",
  "Model": "500, 2012 onwards",
  }
]
}
&#13;
&#13;
&#13;

0 个答案:

没有答案