无法设置默认值 UIPicker swift 2

Posted

技术标签:

【中文标题】无法设置默认值 UIPicker swift 2【英文标题】:Can't set default value UIPicker swift 2 【发布时间】:2016-05-04 10:38:34 【问题描述】:

我有一个 swift 项目,其中包含来自 tableview 的自定义单元格,并且在其中我有一个标签来显示 UIPicker 的值。当您按下标签时,UiPicker 会出现在键盘中,我无法为我的 UIPicker 设置默认值,我尝试了很多不同的技术,有两种方法: selectRow(),没有成功,这是我的 CustomCell 代码:

import UIKit

class CustomCell: UITableViewCell, UIPickerViewDataSource, UIPickerViewDelegate 


    @IBOutlet weak var answer: UITextField!

    var delegate: QuestionSelectorCellDelegate?
    var pickOption: [String] = []
    var pickerView:UIPickerView  = UIPickerView()

    override func awakeFromNib() 
        super.awakeFromNib()
        // Initialization code
    



    @IBAction func editBeginQuestionSelectorCell(sender: UITextField) 
        pickerView = UIPickerView()
        pickerView.delegate = self
        sender.inputView = pickerView
    


    func displayBlock(block: Block)
        if block.answers != nil 
            pickOption = block.answers! // block.answers! = ["blue","red","green"] for example
        

    


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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        return pickOption.count
    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        return pickOption[row]
    

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        if row < pickOption.count 
            answer.text = pickOption[row]
            delegate?.updateQuestionSelectorCell(self, rep: pickOption[row])
        
    


【问题讨论】:

【参考方案1】:

更新了您的代码。 您没有设置 pickerView.dataSource! 如果我理解您的问题,您希望默认选择一个元素,而不是索引 0 处的元素。 我也加了。

import UIKit

class CustomCell: UITableViewCell, UIPickerViewDataSource, UIPickerViewDelegate 


    @IBOutlet weak var answer: UITextField!

    var delegate: QuestionSelectorCellDelegate?
    var pickOption: [String] = []
    var pickerView:UIPickerView  = UIPickerView()

    override func awakeFromNib() 
        super.awakeFromNib()
        // Initialization code
    



    @IBAction func editBeginQuestionSelectorCell(sender: UITextField) 
        pickerView = UIPickerView()
        pickerView.delegate = self

        // --- Thats what I added ---
        pickerView.dataSource = self
        let indexOfDefaultElement = 1 // Make sure that an element at this index exists
        pickerView.selectRow(indexOfDefaultElement, inComponent: 0, animated: false)

        sender.inputView = pickerView
    


    func displayBlock(block: Block)
        if block.answers != nil 
            pickOption = block.answers! // block.answers! = ["blue","red","green"] for example
        

    


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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        return pickOption.count
    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        return pickOption[row]
    

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        if row < pickOption.count 
            answer.text = pickOption[row]
            delegate?.updateQuestionSelectorCell(self, rep: pickOption[row])
        
    

【讨论】:

以上是关于无法设置默认值 UIPicker swift 2的主要内容,如果未能解决你的问题,请参考以下文章

Swift:使用 UIPicker 自定义 DatePicker

Swift UI 中的 UIPicker

使用选定的 UIPicker 行作为自定义 UITableViewCell UILabel SWIFT 的文本

用 contentsOfDirectory Swift 填充 uipicker

swift 可选项(Optional)

Swift 初学者,出现错误 UIPicker