[英]Get the uipickerview value from a .xib file
我是 Swift 的新手(不到一周)我創建了一個表格視圖,其中有更多單元格,每個單元格都是從 a.xib 文件創建的,並且字段被填充,因為我創建了一個按順序使用的對象數組填充單元格。
我有一個名為:MenuWeekViewControoler.swift 的文件,其中包含 tableView。
我有一個名為 FoodTableViewCell.swift 的文件,它與 .xib 文件相連
在 FoodTableViewCell 中,我有 uipickerview,在 MenuWeekViewControoler 中,我可視化了 pickerview 並與之交互。
我的願望是為每個單獨的單元格獲取選擇器視圖的值,但我真的不知道該怎么做。
我將附上這 3 個文件的代碼,以使代碼有意義:
菜單周視圖控制器:
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])
}
}
菜單結構:
import UIKit
struct Menu{
var nameMenu : String
var priceMenu : Int
var pickerDada : [String] = [String]()
}
感謝所有願意提供幫助的人
在FoodTableViewCell中,您應該定義一個委托協議並將其定義為一個變量,例如:
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])
}
}
之后你必須將它委托給你的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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.