PickerView load up tableview data Swift

有些话、适合烂在心里 提交于 2019-12-08 07:26:58

问题


Currently, my uipicker has 3 selections, Cameron, Shaffer and Hydril. When I pick Cameron, i want my uitableview to load up camerondata1. If i pick Shaffer, I want it to load up camerondata2.

How do I setup my UItableview to readjust according to the selection made on my picker view? I new to Swift programming.

class Picker2: UIViewController, UIPickerViewDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate

    {
        var activeTextField:UITextField?
        let textCellIdentifier = "TextCell"
        let camerondata1 = ["Working Pressure (psi): 5,000", "Fluid to Close (gal):  1.69",]

        let camerondata2 = ["Working Pressure (psi): 10,000", "Fluid to Close (gal):  2.69",]

        @IBOutlet var pickerView1: UIPickerView!

        @IBOutlet var textField1: UITextField!

        var brand = ["Cameron","Shaffer", "Hydril"]

        override func viewDidLoad() {

            super.viewDidLoad()
            pickerView1 = UIPickerView()
            pickerView1.tag = 0
            pickerView1.delegate = self
            self.view.addSubview(textField1)
        }

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

        func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

            if pickerView.tag == 0 {

                return brand.count
            }

            return 1
        }

        func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

            if pickerView.tag == 0 {

                return brand[row]
            }
            return ""
        }
        func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {

            if pickerView.tag == 0 {

                textField1.text = brand[row]
            }
        }
        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return camerondata1.count
        }

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell

            let row = indexPath.row

            cell.textLabel?.text = camerondata2[row]

            return cell
        }
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

            tableView.deselectRowAtIndexPath(indexPath, animated: true)
            let row = indexPath.row
            println(camerondata1[row])
        }
    }

回答1:


I'm assuming you have a tableView within the same view as your pickerView (if that's what you're doing you need to make an outlet for your table view and connect it to the delegate and data source). My answer is based off of this assumption.

Basically, just put a conditional statement within your cellForRowAtIndexPath: method. One way you could do this is to declare a variable to hold whichever picker value is currently selected:

var pickerIdentifier: String?

Then, within your pickerView(_: didSelectRow:) method, set the pickerIdentifier to the selection's value.

Finally, write a conditional within your dequeueReusableCellWithIdentifier method to populate the cell with the proper camerondata array values depending on the value of pickerIdentifier.

If in the future the number of values within cameronadata1 and camerondata2 don't match, you'll need to set another conditional on your numberofRowsInSection: method.

Always think what you want to do and break it down. If you want to change the text values of a table row cell depending on something else, then you want to go to where table view cells are created (or dequeued). Also, if it's dependent on something else then use a conditional. If your table view cells aren't showing anything, then you haven't hooked up your table view properly.

class ViewController: UIViewController, UIPickerViewDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
var pickerIdentifier: String?
let textCellIdentifier = "TextCell"
let camerondata1 = ["Working Pressure (psi): 5,000", "Fluid to Close (gal):  1.69",]
let camerondata2 = ["Working Pressure (psi): 10,000", "Fluid to Close (gal):  2.69",]

@IBOutlet var pickerView1: UIPickerView!
@IBOutlet weak var tableView: UITableView!

var brand = ["Cameron","Shaffer", "Hydril"]

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

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
        pickerIdentifier = brand[row]
        tableView.reloadData()
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pickerIdentifier == "Cameron" ? camerondata1.count : camerondata2.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell

    if pickerIdentifier == "Cameron" {
        cell.textLabel!.text = camerondata1[indexPath.row]
    } else {
        cell.textLabel!.text = camerondata2[indexPath.row]
    }
    return cell
}
}


来源:https://stackoverflow.com/questions/28966278/pickerview-load-up-tableview-data-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!