如何重新加载和重置UIPickerView - Swift中的行数

时间:2014-12-29 21:30:00

标签: ios arrays swift uipickerview

我有一个自定义选择器视图,我需要占用我从解析中检索的数组中的元素。但是,因为在从解析中检索数组之前定义了选取器中的元素数,所以选择器轮显示为空。如何重新加载选择器视图中的内容和行数?这是我的代码:

var dates = [String]() 
var locations = [String]() 

@IBOutlet weak var myPicker: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    var query = PFUser.query()
    query.getObjectInBackgroundWithId(PFUser.currentUser().objectId) { (object: PFObject!, error: NSError!) -> Void in
        self.dates = object["userLocationsDate"] as [String]
        self.locations = object["userLocationsCity"] as [String]
    }

   myPicker.reloadAllComponents()

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

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

    return dates.count

}

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

    return dates[row]    
}

1 个答案:

答案 0 :(得分:2)

您需要在getObjectInBackgroundWithId块/闭包内重新加载选择器视图,该窗口是异步调用的。我不知道该完成块是在后台队列还是主队列上运行,但如果它是前者,则还必须将重新加载分配回主队列。