-1

I have a Table which populates data from SQL into TableCells. When i click on a Cell, I want the data of only that particular cell to be passed to a different UIViewController. Data includes Strings. How do I go about this?

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116

2 Answers2

1

Lets make it easy for future searchers to reach this answer-

Transferring data between UITableViewController to UIViewController

Method 1- using storyboard (less code)

Starting scene-

Table View Controller (embedded in UINavigationController) - that's the first ViewController we'll see when we'll open the app, we want him to transfer data to our Destination View Controller

Destination View Controller- that's the ViewController we'll reach after selecting a cell.

You can see in the following GIF that I've set a segue from our TableViewCell to Destination View Controller, the segue's identifier is toDestination (can't see the blue line when dragging? hold ctrl while doing that)

Setting up segue

That's how your storyboard scene should look like-

(don't forget to set your cell's reuse identifier so you can use it later in code)

starting scene Now lets dive into code:

In our TableViewController we write this function:

// Here we're transfering the data we want to our DestinationViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destinationVC = segue.destination as? DestinationViewController{

        if let pressedIndexPath = tableView.indexPathForSelectedRow{
            // passed string is just some variable I've declared on DestinationViewController
            destinationVC.passedString = dataHolder[pressedIndexPath.row]
        }
    }
}

When did you even call this function?- I didn't call it, it's being called automatically because I've set a segue between our cell to our DestinationViewController using our storyboard (remember the starting scene?...), now every time the user presses that cell a segue starts automatically and this function is being called

Our DestinationViewController side looks like that-

class DestinationViewController: UIViewController {

      @IBOutlet weak var label: UILabel!

      var passedString: String!

       override func viewDidLoad() {
          super.viewDidLoad()

          label.text = passedString
       }
}

We're done, here's the final result enter image description here

Full code:

class TableViewController: UITableViewController {

   let dataHolder = ["string1", "string2", "string3", "string4"]

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

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


   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

       // change "cell" to your cell's reuse identifier
       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

       cell.textLabel?.text = dataHolder[indexPath.row]

       return cell
   }

   // Here we're transfering the data we want to our DestinationViewController
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

       if let destinationVC = segue.destination as? DestinationViewController{

           if let pressedIndexPath = tableView.indexPathForSelectedRow{

               destinationVC.passedString = dataHolder[pressedIndexPath.row]
           }
       }
    }
}

And on the other side:

class DestinationViewController: UIViewController {

      @IBOutlet weak var label: UILabel!

      var passedString: String!

       override func viewDidLoad() {
          super.viewDidLoad()

          label.text = passedString
       }
}
Niv
  • 517
  • 5
  • 18
0

You can create a custom cell like this

class CustomCell: UITableViewCell {
    var yourData: CustomData? // You can put your data here as you want, you can also have three different variables for your data

    override func awakeFromNib() {
        // Initialization code
        super.awakeFromNib()
    }
}

After that in function cellForRow

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! CustomCell

    // Here you can set data of your cell as you want
    cell.yourData = // Put your data

    return cell
}

And then you can pass your data using didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedCell: CustomCell = tableView.cellForRow(at: indexPath)!
    let yourData = selectedCell.yourData
    // Here you can perform a segue or you can present a ViewController and pass your data
    ....
}