-1

I am VERY new to Swift and trying to learn as I write my first simple app. I have a storyboard with a custom picker on the main ViewController and I need to populate table cells in a second TableViewController with picker selections from the first. Basically, the user will make a selection from a picker in the the first view, then click an "Add" button which will take their selection and add it to the table in the second view. They can then follow the same process for additional selections. I could use a list or something else for this too so I'm not married to using a table.

Can anyone lead me to the right direction on how to do this? Please keep in mind that I am new and trying to learn here. I can't seem to find anything so far that will help me. Thanks!

gwpeaks
  • 13
  • 4
  • Show the code that you have already tried. – PGDev Sep 10 '17 at 14:20
  • And what have you tried so far? – Ahmad F Sep 10 '17 at 14:37
  • As of now I've only tried what PGDev posted below. Again, I'm brand new to Swift. I'm combing through the posts that rmaddy referenced above and will post the solution as soon as I have it. Thanks for the responses so far. – gwpeaks Sep 10 '17 at 17:36

1 Answers1

0

You simply need to fetch the data of selected row in your custom UIPickerView and send it to the UITableViewController, i.e.

1. Fetching and passing data

var tableController: TableViewController?

@IBAction func addItem(sender: UIButton)
{
    if let tableController = self.tableController
    {
        tableController.items.append(items[self.picker.selectedRow(inComponent: 0)])
        tableController.tableView.reloadData()
        self.navigationController?.pushViewController(tableController, animated: true)
    }
}

2. UITableViewController

class TableViewController: UITableViewController
{
    var items = [String]()

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = items[indexPath.row]
        return cell
    }
}
PGDev
  • 23,751
  • 6
  • 34
  • 88