How to use UIPickerView inside a UITableViewController

Hi, I have a view table controller name MyTableViewController. In MyTableViewControllerI use different UIPickerViewinside different celltables, which is called the name headerCell, which is set by the following function and in this I do not useUIPickerView

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let  headerCell = tableView.dequeueReusableCell(withIdentifier: "header") as! MyTableViewCell
    headerCell.contentView.backgroundColor = UIColor.white
    return headerCell
}

MyTableViewCell- is a class type UITableViewCell, which I tied all the tables, such as Label, TextField, Button, UIPickerViewetc.

My second cell is middleCella footerCell, which is equal to

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell : SubscriptionTableViewCell?
    if indexPath.row == 1 {
        cell = tableView.dequeueReusableCell(withIdentifier: "footer", for: indexPath) as? SubscriptionTableViewCell
        cell!.cardTypePicker.delegate = self
        cell!.expMonth.delegate = self
        cell!.expYear.delegate = self
        cell!.countryPicker.delegate = self
        cell!.shippingMethodPicker.delegate = self

        cell!.cardTypePicker.dataSource = self
        cell!.expMonth.dataSource = self
        cell!.expYear.dataSource = self
        cell!.countryPicker.dataSource = self
        cell!.shippingMethodPicker.dataSource = self

    }else {
        cell = tableView.dequeueReusableCell(withIdentifier: "middle", for: indexPath) as? SubscriptionTableViewCell
        cell!.variantPicker.delegate = self
        cell!.variantPicker.dataSource = self
    }
    return cell!
}

as you can see, there are different ones UIPickerView. So now I can’t understand how to use delegates UIPickerViewinside UITableViewController.

Please help for this.

+4
3

TableViewCell: UITableViewCell {

@IBOutlet var pickerView: UIPickerView!

var arrCountryName : [String] = []

override func awakeFromNib() {
    super.awakeFromNib()

    arrCountryName = ["India" , "Australia"]
    pickerView.delegate = self
    pickerView.dataSource = self
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

extension TableViewCell: UIPickerViewDataSource, UIPickerViewDelegate {

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

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

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

}

+1

, , Inline Picker. . UIPickerView UITableViewCell , , . , :

https://github.com/sebamisc/UItableViewWithPicker

+1

UIView , tag, . , .

, - .

EDIT ( ):

You do not need to assign the same delegate to each collector. You can create new classes that implement the delegate protocol.

So,

class CountryPickerDelegate: UIPickerDelegate {

// do what the country picker needs
// The init can take the VC if you need that.
// add a weak var viewController: MyTableViewController? = nil 

}

class CardTypePickerDelegate: UIPickerDelegate {

// do what the card picker needs
// The init can take the VC if you need that.
// add a weak var viewController: MyTableViewController = nil

}

Then

cell!.countryPicker.delegate = CountryPickerDelegate(viewController: self)
0
source

Source: https://habr.com/ru/post/1673506/


All Articles