如何使用表中的单元格数据作为UIPickerView中的选择

时间:2019-04-22 20:01:40

标签: ios swift uitableview uipickerview

基本上,我在名为 FirstViewController 的视图控制器中有一个表,该表包含各种条目的列表,每个单元格只有几个单词。我想将这些条目显示为UIPickerView的视图控制器上的选项:

CreateRMA_ViewController

FirstViewController 中表的数据来自MySQL数据库,因此我宁愿从表中填充UIPickerView(在数据已经从数据库中下载之后),而不是重新下载UIPickerView的数据相同。

这是表格如何获取其单元格数据的方法:

import UIKit

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, FeedModelProtocol{

    var feedItems: NSArray = NSArray()
    var selectedStock : StockModel = StockModel()
    let tableView = UITableView()
    @IBOutlet weak var listTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //set delegates and initialize FeedModel


        self.listTableView.delegate = self
        self.listTableView.dataSource = self

        let feedModel = FeedModel()

        feedModel.delegate = self
        feedModel.downloadItems()

    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }


    func itemsDownloaded(items: NSArray) {

        feedItems = items
        self.listTableView.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of feed items

        print("item feed loaded")
        return feedItems.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // Retrieve cell
        let cellIdentifier: String = "stockCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        myCell.textLabel?.textAlignment = .center
        myCell.textLabel?.font = .boldSystemFont(ofSize: 18)
        // Get the stock to be shown
        let item: StockModel = feedItems[indexPath.row] as! StockModel
        // Configure our cell title made up of name and price


        let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")


        print(titleStr)
        // Get references to labels of cell
        myCell.textLabel!.text = titleStr

        return myCell
    }

如何将每个单元格中的数据作为UIPickerView的选择?

这是我过去编写UIPickerViews的方式:

var pickerView: UIPickerView!

    let gradePickerValues = ["Tap to Select Time","8:00 am -12:00 pm", "12:00 pm - 4:00 pm", "4:00 pm - 8:00 pm"]


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


    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        if gradeTextField.isFirstResponder {
            return gradePickerValues.count
        } else {
            return stopPickerValues.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if gradeTextField.isFirstResponder {
            return gradePickerValues[row]
        } else {
            return stopPickerValues[row]
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self

        stopTextField.inputView = pickerView
        gradeTextField.inputView = pickerView
        gradeTextField.text = gradePickerValues[0]
        stopTextField.text = stopPickerValues[0]
} 

如何将这些硬编码的值更改为表视图的值?

0 个答案:

没有答案