多选选择器视图不能在一个选项中拥有更多选项

时间:2016-03-15 12:33:30

标签: swift swift2 uipickerview

目的:

  • 设置选择器以选择年龄,球员号码和局?
  • 年龄值应为8,9,10
  • 的数字值应为0-99
  • 局的值应为1-6

问题:

当我测试它时,如果数组中有更多它们没有显示,并且它的崩溃次数减少,则每个选项中只显示3个选项。

的问题:

  • 如何为每个项目显示正确数量的选项?
  • 如何使0-99选择器变为0,1,2 ...而不必列出每个单独的数字,如0 ... 99(我试过这个并且它不起作用)?

代码:

import UIKit

class PitcherPickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var picker: UIPickerView!

     var pickerData: [[String]] = [[String]]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        // Connect data:
        self.picker.delegate = self
        self.picker.dataSource = self

        // Input data into the Array:
        pickerData = [["8", "9", "10"],
            ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "91", "92", "93", "94", "95", "96", "97", "98", "99"],
            ["1", "2", "3", "4", "5", "6"]]
    }

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


    // The number of columns of data
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 3
    }

    // The number of rows of data
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    // The data to return for the row and component (column) that's being passed in
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[component][row]
    }

    // Catpure the picker view selection
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        // This method is triggered whenever the user makes a change to the picker selection.
        // The parameter named row and component represents what was selected.
    }


}

1 个答案:

答案 0 :(得分:0)

pickerData是一个数组数组

尝试以下方法:

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData[component].count
}

如果您不想要连续数字,请相应地更改pickerData,因为这是titleForRow委托方法中使用的内容。