使用swift在选择器视图中填充解析对象

时间:2015-03-26 19:16:02

标签: ios swift parse-platform uipickerview

希望有人可以帮忙吗? 我对一个视图有两个查询。 第一个填充一个按预期工作的表。 我要填充UIPickerView的第二个查询,这让我感到悲伤:(

我可以在println语句中看到结果 如果有人可以提供帮助,我们将不胜感激

这是代码     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()
        }
    }

}

提前致谢

1 个答案:

答案 0 :(得分:1)

您应该在self.countryControl.reloadAllComponents()内取消注释findObjectsInBackgroundWithBlock,以便您的`UIPickerView可以刷新。

此外,您需要使您的UIViewControl扩展UIViewController,UIPickerViewDataSourceUIPickerViewDelegate。执行以下操作:

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
}

然后在viewDidLoad()方法中,设置UIPickerView

的委托和数据源
override func viewDidLoad() {
    super.viewDidLoad()
    countryControl.delgete = self
    countryControl.dataSource = self
......}

您还应该为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]    
}