0

I'm trying to segue from one UITableView to another UITableView. I want to segue and pass the myCell.textLabel?.text value of the selected cell to the second UITableView.

The code for my first UITableView (MenuTypeTableViewController and the code for my second UITableView (TypeItemsTableViewController) is also below.

I'm fully aware this involves the prepareForSegue function which currently I've not created, purely because I'm unsure where I override it and how to pass in the textLabel value to it.

Hope my question makes sense, I will update with suggestions and edits.

class MenuTypeTableViewController: UITableViewController, MenuTypeServerProtocol {

    //Properties
    var cellItems: NSArray = NSArray()
    var menuType: MenuTypeModel = MenuTypeModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        let menuTypeServer = MenuTypeServer()
        menuTypeServer.delegate = self
        menuTypeServer.downloadItems()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier: String = "cellType"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        let item: MenuTypeModel = cellItems[indexPath.row] as! MenuTypeModel
        myCell.textLabel?.text = item.type
        return myCell
    }


    func itemsDownloaded(items: NSArray) {
        cellItems = items
        tableView.reloadData()
    }
}



class TypeItemsTableViewController: UITableViewController, TypeItemsServerProtocol {

    //Properties
    var cellItems: NSArray = NSArray()
    var typeItemList: TypeItemsModel = TypeItemsModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        let typeItemsServer = TypeItemsServer()
        typeItemsServer.delegate = self
        typeItemsServer.downloadItems()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier: String = "cellTypeItem"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        let item: TypeItemsModel = cellItems[indexPath.row] as! TypeItemsModel
        myCell.textLabel?.text = item.name
        return myCell
    }

    func itemsDownloaded(items: NSArray) {
        cellItems = items
        tableView.reloadData()
    }
}
Shades
  • 5,568
  • 7
  • 30
  • 48

3 Answers3

1

Hi try the following set of code, I have added few additional changes in your code make use of it, I hope it will solve your issue.
I have added only the extra codes which you needed

class TypeItemsTableViewController: UITableViewController, TypeItemsServerProtocol {

// Add this variable in this class and use it whereever you needed it in this class
var selectedItem: String?


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Get the selected cell
    let selectedCell = tableView.cellForRow(at: indexPath)

    // Now maintain the text which you want in this class variable
    selectedItem = selectedCell?.textLabel?.text

    // Now perform the segue operation
    performSegue(withIdentifier: "TypeItemsTableViewController", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "TypeItemsTableViewController" {
        let destinationVC = segue.destination as? TypeItemsTableViewController
        destinationVC?.selectedItem = self.selectedItem // Pass the selected item here which we have saved on didSelectRotAt indexPath delegate
    }
}

In Second class:

class TypeItemsTableViewController: UITableViewController, TypeItemsServerProtocol {


// Add this variable in this class and use it whereever you needed it in this class
var selectedItem: String?
Bharath
  • 2,064
  • 1
  • 14
  • 39
0

What you can do is to make a variable in your second UITableView

var String: labelSelected?

then in you prepare for segue method just set the labelSelected to the value of the cell.

refToTableViewCell.labelSelected = youCell.textlabel?.text
Siyavash
  • 970
  • 9
  • 23
0

If you set up a segue in storyboards from one storyboard to another, you can use the code below in your prepareForSegue method. You'll need to add a testFromMenuTableViewController property to your TypeItemsTableViewController.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? TypeItemsTableViewController,
        let path = self.tableView.indexPathForSelectedRow,
        let cell = self.tableView.cellForRow(at: path),
        let text = cell.textLabel?.text {
        destination.textFromMenuTypeTableViewController = text
    }
}

For more info check this SO answer.

Blake
  • 122
  • 1
  • 7