简体   繁体   English

UIPickerView为什么不显示行? 迅速

[英]Why isn't UIPickerView showing rows? swift

I'm trying to make a single UIPickerView with 2 components, each with a different amount of rows. 我正在尝试使用2个组件制作单个UIPickerView,每个组件具有不同数量的行。 I thought that I could base the row number per component on the index of the component, but when I run my app the UIPickerView displays only one single blank row and doesn't allow scrolling. 我以为可以在每个组件的索引上建立每个组件的行号,但是当我运行我的应用程序时,UIPickerView仅显示一个空白行,并且不允许滚动。

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

// grades is a 2D array
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return grades[component].count
}

// level and letter are predefined ints
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if component == 0 {
        level = row
    }else if component == 1 {
        letter = row
    }
    return ""
}

EDIT: 编辑:

This is my viewDidLoad() 这是我的viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()
    pickerView.dataSource = self;
    pickerView.delegate = self;
}

I believe you should return non-empty title to make it work 我相信您应该返回非空标题以使其起作用

...

// level and letter are predefined ints

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

    if component == 0 {
        level = row
    } else if component == 1 {
        letter = row
    }
    return "Component: \(component) Row: \(row)" // whatever string you want to display
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM