简体   繁体   English

带有动态内容 Swift 的 UIPicker

[英]UIPicker with dynamic Content Swift

I have a table view with a button.我有一个带按钮的表格视图。 The table view also contains a quantity figure like this:表格视图还包含一个数量图,如下所示:

Class      Price      Qty     Action
Regular    $5         10       Buy

When the buy action is clicked, I'd like to show a uipickerview with number from 1-10 as the content of the picker so the use can select how many tickets they'd like to buy.单击购买操作时,我想显示一个 uipickerview,其中编号为 1-10 作为选择器的内容,以便用户可以选择他们想要购买的门票数量。 My problem is that with the normal UIPicker, I'd have to conform to the delegate and set the data source but the data source is dependent on the table view clicked.我的问题是,对于普通的 UIPicker,我必须遵守委托并设置数据源,但数据源取决于单击的表视图。 Any idea how to get around this?知道如何解决这个问题吗?

You're going to create an array that changes its content according to the clicked button then reload the picker, the datasource of the picker is changeable not static as you might think, so implement these methods and change contentsArr every button click you want to show data.您将创建一个数组,根据单击的按钮更改其内容,然后重新加载选择器,选择器的数据源是可变的,而不是像您想象的那样静态,因此请实现这些方法并更改contentsArr每次单击要显示的按钮数据。

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

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

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

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    }

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

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