Byte Byte - 5 months ago 30
iOS Question

How to share data of tableViewCell to next viewController without using storyboard in ios with Swift

I am using Xib files in my project for building interface of my app.
I have a tableView in my first viewController from which I want to pass data to next ViewController. I have created a custom cell for my tableView which contains an imageView and two labels.

This is my code

import UIKit

class YASProductListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

// registering my custom cell
tableView.registerNib(UINib(nibName: "YASProductListTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "cell")
let cell : YASProductListTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! YASProductListTableViewCell
cell.productNameLabel.text = prodcutNames[indexPath.row]
cell.productDetailLabel.text = productDetail[indexPath.row]

return cell
}

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

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

return 140
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! YASProductListTableViewCell

let destination = YASProductDetaiilViewController(nibName: "YASProductDetaiilViewController", bundle: NSBundle.mainBundle())

destination.productImage = cell.productImageView.image
destination.productTitle = cell.productNameLabel.text!

let productDetails = YASProductDetaiilViewController(nibName: "YASProductDetaiilViewController", bundle: nil) as YASProductDetaiilViewController
navigationController?.navigationBarHidden = false
navigationController?.title = ""
navigationController?.pushViewController(productDetails, animated: true)

}


Now what I want to do is pass the image and labels text to next viewController when user tap on any cell. Here is the code of next ViewController

import UIKit

class YASProductDetaiilViewController: UIViewController {
@IBOutlet weak var productImageView: UIImageView!
@IBOutlet weak var productTitleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
setupViewControllerUI()
// Do any additional setup after loading the view.
}

// MARK: - UIViewController helper Methods

func setupViewControllerUI(){

productImageView.image = productImage
productTitleLabel.text = productTitle
}
}


As you can see I have tried it didSelectRowAtIndexPath but its not working. Please help! Thanks

Answer

Change your didSelect method like below code,

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! YASProductListTableViewCell

let productDetails = YASProductDetaiilViewController(nibName: "YASProductDetaiilViewController", bundle: nil) as YASProductDetaiilViewController
navigationController?.navigationBarHidden = false
navigationController?.title = ""
productDetails.productImage = cell.productImageView.image
productDetails.productTitle = cell.productNameLabel.text!
navigationController?.pushViewController(productDetails, animated: true)

}

Hope this helps you.