作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在我的项目中使用 Xib 文件来构建我的应用程序界面。我的第一个 viewController 中有一个 tableView,我想从中将数据传递给下一个 ViewController。我为我的 tableView 创建了一个自定义单元格,其中包含一个 imageView 和两个标签。
这是我的代码
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)
}
现在我要做的是在用户点击任何单元格时将图像和标签文本传递给下一个 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
}
}
如您所见,我已经尝试了 didSelectRowAtIndexPath 但它不起作用。请帮忙!谢谢
最佳答案
您正在使用正确的方法在 viewController 之间共享数据。然而你犯了一个错误。您正在创建 ProductDetailViewController 的两个实例。您只需创建一个目标 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)
}
我希望它能奏效。
关于ios - 如何将 tableViewCell 的数据共享到下一个 viewController 而无需使用 Swift 在 ios 中使用 Storyboard ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38091338/
我是一名优秀的程序员,十分优秀!