Search code examples
iosswiftparse-platformuipickerview

Populating parse object in picker view with swift


Hope someone can help? I have two queries on a single view. The first populates a table that works as intended. The second query I want to populate a UIPickerView and this is causing me grief :(

I can see the results in the println statement If someone can help it would be greatly appreciated

Here is the code class ProfileVC: UIViewController {

var pickerString = NSArray() as AnyObject as [String]
var industryValue = [getIndustries]()

@IBOutlet weak var StoredIndustries: UITableView!
@IBOutlet weak var countryControl: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    var query = PFQuery(className:"Industries")

    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in

        if error == nil {
            // query successful - display number of rows found
            println("Successfully retrieved \(objects.count) industries")

            // print sentences found
            for object in objects {

                let retrievedIndustries = object["Sector"] as NSString
                self.industryValue += [getIndustries(parseIndustry:"\(retrievedIndustries)")]
                println("\(retrievedIndustries) ")
            }

        } else {
            // Log details of the failure
            println("Error: \(error) \(error.userInfo!)")
        }

        self.StoredIndustries.reloadData()
    }

    var query2 = PFQuery(className: "CountryLanguage")

    query2.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in

        if error == nil {
            println("Successfully retrieved \(objects.count) Countries")
            var counter: Int = 0
            for object in objects {
                var storeString = ""
                let retrievedLanguages = object["Language"] as NSString
                let retrievedCode = object["Ccode"] as NSString
                println("\(retrievedLanguages)" + " - " + "\(retrievedCode)")
                storeString = ("\(retrievedLanguages)" + " - " + "\(retrievedCode)")
                self.pickerString.insert(storeString, atIndex:counter)
                counter = counter + 1
            }
            //self.countryControl.reloadAllComponents()
        }
    }

}

Thanks in advance


Solution

  • You should uncomment self.countryControl.reloadAllComponents() inside your findObjectsInBackgroundWithBlock, so that your `UIPickerView can refresh.

    Also you need to make your UIViewControl extends the UIViewController,UIPickerViewDataSource and UIPickerViewDelegate. Do the following:

    class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
    }
    

    Then in the viewDidLoad() method, set the delegate and datasource of your UIPickerView

    override func viewDidLoad() {
        super.viewDidLoad()
        countryControl.delgete = self
        countryControl.dataSource = self
    ......}
    

    Also you should implement the following delegate methods for UIPickerView:

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
       return self.pickerString.count
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    
        return self.pickerString[row]    
    }