简体   繁体   中英

Get the uipickerview value from a .xib file

I am very new to Swift (less than one week) I create a table view where I have more cells, each cell gets create from a.xib file, and the fields get populated because I create an array of objects that are used in order to populate the cells.

I have a file called: MenuWeekViewControoler.swift where I have the tableView.

I have a file called FoodTableViewCell.swift which is connected with the.xib file

Inside the FoodTableViewCell I have the uipickerview and in the MenuWeekViewControoler I visualise the pickerview and interact with it.

My wish is to get the value of the picker view for each separate cell and I don't really know how to do it.

I will attach the code of the 3 files in order for the code to make sense:

MenuWeekViewControoler:

import UIKit

class MenuWeekViewController :  UIViewController, UITableViewDelegate,  UITableViewDataSource {
   
    
    var menus : [Menu] = [
        Menu(nameMenu: "BBQ", priceMenu: 8, pickerDada: ["0","1","2","3","4","5","6","7","8","9","10"]),
        Menu(nameMenu: "BBQ2", priceMenu: 8, pickerDada: ["0","1","2","3","4","5","6","7","8","9","10"]),
        Menu(nameMenu: "BBQ3", priceMenu: 8, pickerDada: ["0","1","2","3","4","5","6","7","8","9","10"]),
        Menu(nameMenu: "BBQ4", priceMenu: 8, pickerDada: ["0","1","2","3","4","5","6","7","8","9","10"]),
        Menu(nameMenu: "BBQ4", priceMenu: 8, pickerDada: ["0","1","2","3","4","5","6","7","8","9","10"]),
        Menu(nameMenu: "BBQ4", priceMenu: 8, pickerDada: ["0","1","2","3","4","5","6","7","8","9","10"]),
        
    ]
    
   
    
    var test = FoodTableViewCell()
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var nameSection: UITextField!
    @IBOutlet weak var privateGuestsUIPicker: UIPickerView!
    @IBOutlet weak var BusinessGuestUIPicker: UIPickerView!
    @IBOutlet weak var commentSection: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
     
        tableView.dataSource = self
        tableView.delegate = self
      
        tableView.rowHeight = 100
        tableView.register(UINib(nibName: "FoodTableViewCell", bundle: nil), forCellReuseIdentifier: "ReusableMenuCell")
    }
    
    @IBAction func updateOrders(_ sender: UIButton) {
        
    }
    
    
    @IBAction func sendOrder(_ sender: UIButton) {
   
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return menus.count
   }
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableMenuCell", for: indexPath) as! FoodTableViewCell
        cell.menuName?.text = menus[indexPath.row].nameMenu
        cell.priceMenu?.text = String("\(menus[indexPath.row].priceMenu) CHF")
       return cell
   }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        print(menus[indexPath.row].nameMenu)
    }  
}

FoodTableViewCell:

import UIKit

class FoodTableViewCell: UITableViewCell,UIPickerViewDelegate, UIPickerViewDataSource  {
   
    var pickerDada = ["0","1","2","3","4","5","6","7","8","9","10"]
    
    @IBOutlet weak var quantityMenu: UIPickerView!
    @IBOutlet weak var priceMenu: UILabel!
    @IBOutlet weak var menuName: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        quantityMenu.dataSource = self
        quantityMenu.delegate = self
        quantityMenu.setValue(UIColor.white, forKey: "textColor")
      
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 11
    }
    
    internal func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
          return pickerDada[row]
      }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
       {
           print(pickerDada[row])
        }
}

Menu struct:

import UIKit

struct Menu{
    var nameMenu : String
    var priceMenu : Int
    var pickerDada : [String] = [String]()
}

Thank you to everyone willing to help

in FoodTableViewCell you should define a delegate protocol and define it as a variable such as:

import UIKit

protocol FoodCellDelegate: AnyObject {
    func tapFood(food: YourFoodModel) or // func tapFood(food: pickerDada) or whatever you want pass it
}

class FoodTableViewCell: UITableViewCell,UIPickerViewDelegate, UIPickerViewDataSource  {

    //define a variable type of delegate
    weak var delegate: FoodCellDelegate?
   .
.
.
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
       {
           print(pickerDada[row])
    // add this 
           delegate?.tapFood(food: pickerData[row])
        }
}

after that you must delegate it to your MenuWeekViewController :

class MenuWeekViewController :  UIViewController, UITableViewDelegate,  UITableViewDataSource {
   
    .
.
.
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableMenuCell", for: indexPath) as! FoodTableViewCell
        cell.menuName?.text = menus[indexPath.row].nameMenu
        cell.priceMenu?.text = String("\(menus[indexPath.row].priceMenu) CHF")

// add this
       cell.delegate = self
    
       return cell
   }
    .
    .
.

}

extension MenuWeekViewController: FoodCellDelegate {
     func tapFood(food: YourFoodModel) {
        print it
     }
}

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