James Just-Gaming James Just-Gaming - 3 years ago 114
Swift Question

Pass the myCell.textLabel?.text value via a segue in a dynamic prototype

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()
}
}

Answer Source

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?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download