Joe Joe - 2 months ago 14
Swift Question

Passing Data between TableView in Swift

I have two tableView running in my project.I am trying to pass(copy) my first tableViewcell data to second tableView.I using tableView row action method to pass data.My partial code below...

First VC:

var tableView: UITableView!
var DataArray = ["Bus","Helicopter","Truck","Boat","Bicycle","Motorcycle","Plane","Train","Car","S cooter","Caravan"]
var sendSelectedData = NSString()

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

let copyAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Pass Data") { (UITableViewRowAction, NSIndexPath) -> Void in

print("Button Pressed") // Xcode Console prints **Button Pressed** when swipe action performed.

self.performSegue(withIdentifier: "send", sender: self)

}

return [copyAction]
}


func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
self.performSegue(withIdentifier: "send", sender: self)
// segue.destination as! tableController

let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)!
self.sendSelectedData = (currentCell.textLabel?.text)! as String as NSString

let viewController = segue.destination as! tableController
viewController.labelcell = ([self.sendSelectedData as String])
print(self.sendSelectedData) // no result
}


Second VC:

var labelcell = [String]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath as IndexPath) as UITableViewCell


cell.textLabel?.text = labelcell[indexPath.row] as? String

tableView.reloadData()

return cell
}


Above code looks like passing data to my second VC(segue).But, I am only getting a empty tableview..

Answer

Okay after testing it, it turns out, that you're using an incorrect prepareForSegue function. You are not using "prepareForSegue", you are creating a function called prepareForSegue - since the syntax has changed in Swift 3. This one will get called and you can pass data.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "send" {

        let selectedIndex = sender as! NSIndexPath
        let currentCell =  tableView.cellForRow(at: selectedIndex as IndexPath)! as! Cell
        self.sendSelectedData = (currentCell.label?.text)! as String as NSString

        print(self.sendSelectedData) // till here it worked for me - it is filled with my label.text
        // I don't know what this is "viewController.labelcell", so you have to to know how to go on from here on

        viewController.labelcell = ([self.sendSelectedData as String])           
    }
}

Also you need to pass the indexPath:

self.performSegue(withIdentifier: "send", sender: indexPath)

Exactly like this:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let copyAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Pass Data") { (UITableViewRowAction, NSIndexPath) -> Void in

        print("editActionsForRowAt called") // Xcode Console prints **Button Pressed** when swipe action performed.
        self.performSegue(withIdentifier: "send", sender: indexPath)

    }

    return [copyAction]
}

This worked in my testing project.

Also beware: Cell is a custom subclass of UITableViewCell I have created and label is an UIOutlet of a label element for my test project.

Comments