使用UIPickerView从数组中触发表的填充

时间:2016-07-01 23:35:16

标签: ios arrays swift uitableview uipickerview

首先,如果我的思维过程完全错误,我会道歉。我对这一切都很陌生,(说到我的第一行代码还不到两周,说实话!)但我认为我需要的功能应该是可能的

我正在尝试使用UIPickerView来选择40个选项中的一个 - 然后选择的值用于触发包含来自第二个数组的值的小表的填充。

到目前为止,我设法添加并正确配置UIPickerView以读取第一个数组,并自动将选择器中所选项的值返回到UILabel(只是为了证明我从中获取了正确的输出)选取器)

考虑到我的业余性,我对自己很满意。

本质上,这个初始数组包含40个组织的详细信息,我希望在选择一个组织时显示的表将包含两列中的6到8行。第一列将是标题(地址,电话等),第二列将是特定于该组织的值。 列将在所有条目中保持一致,为每个组织返回相同的信息。在这方面,我希望这个问题对于你们的专家来说比现在更简单。

我的代码到目前为止......

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource  {

@IBOutlet weak var orgSelectedFromPicker: UILabel!
@IBOutlet weak var orgPicker: UIPickerView!

//function for the number of columns in the picker
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

//function counting the array to give the number of rows in the picker
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return orgPickerData.count
}

//function displaying the array rows in the picker as a string
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return orgPickerData[row]
}

//function allowing the font and colour of the picker to be changed
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = orgPickerData[row]
    let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Verdana", size: 15.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
    return myTitle
}

//function returning the selected row from the picker
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.orgSelectedFromPicker.text = orgPickerData[row]
}



//Array containing the list for UIPickerView
    var orgPickerData = [...........] //I've taken the values out of here for privacy purposes..........]
override func viewDidLoad() {
    super.viewDidLoad()
    self.orgPicker.dataSource=self
    self.orgPicker.delegate=self

    // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

`

那么 - 任何想法?

0 个答案:

没有答案