简体   繁体   中英

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. 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. 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.

    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) {

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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