[英]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. 我在项目中使用Xib文件来构建应用程序的界面。 I have a tableView in my first viewController from which I want to pass data to next ViewController.
我的第一个viewController中有一个tableView,我想从中将数据传递到下一个ViewController。 I have created a custom cell for my tableView which contains an imageView and two labels.
我为tableView创建了一个自定义单元格,其中包含一个imageView和两个标签。
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. 现在我想做的是当用户点击任意一个单元格时将图像传递并标记文本到下一个viewController。 Here is the code of next ViewController
这是下一个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. 如您所见,我已经尝试过didSelectRowAtIndexPath但它不起作用。 Please help!
请帮忙! Thanks
谢谢
You are using right method to share data between viewController. 您正在使用正确的方法在viewController之间共享数据。 However you have made a mistake.
但是,您犯了一个错误。 You are creating two instance of your ProductDetailViewController.
您正在创建ProductDetailViewController的两个实例。 You need to create only one instance of destination ViewCotroller and then set its properties accordingly you you can simply replace your didSelectRowAtIndexPath method with following
您只需要创建目标ViewCotroller的一个实例,然后相应地设置其属性,就可以用以下命令替换您的didSelectRowAtIndexPath方法
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! YASProductListTableViewCell
let productDetails = YASProductDetaiilViewController(nibName: "YASProductDetaiilViewController", bundle: nil) as YASProductDetaiilViewController
productDetails.productImage = cell.productImageView.image
productDetails.productTitle = cell.productNameLabel.text!
navigationController?.pushViewController(productDetails, animated: true)
}
I hope it will work. 我希望它能起作用。
Change your didSelect method like below code, 像下面的代码一样更改didSelect方法,
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. 希望这对您有所帮助。
Do as follow might be help you. 请执行以下操作可能会对您有所帮助。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! YASProductListTableViewCell
let objYASProductDetaiilViewController = self.storyboard?.instantiateViewControllerWithIdentifier("STRORYBOARD_ID") as? YASProductDetaiilViewController
objYASProductDetaiilViewController.productImage = cell.productImageView.image
objYASProductDetaiilViewController.productTitle = cell.productNameLabel.text!
self.navigationController?.pushViewController(objMedicalDevicesVC!, animated: true)
navigationController?.navigationBarHidden = false
navigationController?.title = ""
navigationController?.pushViewController(objYASProductDetaiilViewController, animated: true)
}
Here You can not assign a image View
to image View
directly.You need to get a image first then it give to a other view controller image then set it to image to UIImage
在这里,您无法分配
image View
到image View
directly.You需要获得一个图像,然后第一次把它给一个其他视图控制器的图像,然后将其设置为图像UIImage
import UIKit
class YASProductDetaiilViewController: UIViewController {
@IBOutlet weak var productImageView: UIImage!
@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
}
}
create dictionary of all the needed information and the add to array and get using indexpath.row when cell is clicked 创建所有必需信息的字典,并添加到数组,并在单击单元格时使用indexpath.row
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var dicTemp=prodcutNames[indexPath.row] as! NSMutableDictionary
let destination = YASProductDetaiilViewController(nibName: "YASProductDetaiilViewController", bundle: NSBundle.mainBundle())
destination.productImage = prodcutNames.valueForKey("imageObj") //get image object
destination.productTitle = prodcutNames.valueForKey("product_title")//get product title
let productDetails = YASProductDetaiilViewController(nibName: "YASProductDetaiilViewController", bundle: nil) as YASProductDetaiilViewController
navigationController?.navigationBarHidden = false
navigationController?.title = ""
navigationController?.pushViewController(productDetails, animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.