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?
-
what is your cell selection type? – Shubham Mishra Nov 29 '18 at 11:44
-
Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Tamás Sengel Nov 29 '18 at 20:24
2 Answers
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)
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)
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
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
}
}

- 517
- 5
- 18
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
....
}

- 343
- 1
- 14