PickerView 只显示 '?' [英] PickerView shows only '?'

查看:37
本文介绍了PickerView 只显示 '?'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的源代码,我所做的是在 storyBoard 上创建选择器视图.通过控制+拖动在此控制器中制作 IBOutlet.

This is my source code and what I have done is make picker view on storyBoard. Make IBOutlet in this controller by contorl+drag.

它可以编译但是,只有'?'出现在选择器视图中.

It can be compiled however, only '?' appears in the picker view.

问题出在哪里?

import UIKit

class SelectViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
    var songNames = ["test1","test2","test3"]

    @IBOutlet weak var songPicker: UIPickerView!

    override func viewDidLoad(){ 
        songPicker.delegate = self
        songPicker.dataSource = self

    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return songNames.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int) -> String? {
        return songNames[row]
    }
}

推荐答案

您遗漏了 dataSource 方法中的 forComponent 参数.将它添加到您的 titleForRow 函数中,如下所示:

You have missed the forComponent parameter from the dataSource method. Add it in your titleForRow function like this:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return songNames[row]
}

这应该可以解决您遇到的问题.

This should fix the problem you are having.

这篇关于PickerView 只显示 '?'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆