Xcode 中无法将数据从集合视图传递到详细信息视图

Cannot pass data from collection view to detail view in Xcode

我是一个 Swift 初学者,试图传递集合视图单元格中包含的产品的相应名称和图像。我创建了一个单例和一个数据文件来模拟从互联网上提取这些信息。我正在努力将集合视图单元格的值分配给详细视图控制器上的插座。我哪里错了?我正在努力了解如何正确分配这些值。这是我的文件:

集合视图文件

import UIKit

class ProductVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.delegate = self
    collectionView.dataSource = self
}

@IBAction func backButtonPressed(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return DataServices.instance.getProducts().count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath) as? ProductCell {
        let product = DataServices.instance.getProducts()[indexPath.row]
        cell.updateView(product: product)
        return cell
    }
    return ProductCell()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ProductDetailVC" {
        var productDetailVC = segue.destination as! ProductDetailVC
        let cell = sender as! UICollectionViewCell
        let indexPath = collectionView.indexPath(for: cell)
        let product = DataServices.instance.getProducts()[(indexPath?.row)!]
    }
}


}

产品详细信息视图控制器

import UIKit

class ProductDetailVC: UIViewController {

@IBOutlet weak var productName: UILabel!
@IBOutlet weak var productImageView: UIImageView!

var product: Product?

override func viewDidLoad() {
    super.viewDidLoad()
}

func updateView() {
    productName.text = product?.name
    productImageView.image = UIImage(named: (product?.imageName)!)
}


}

产品型号

import Foundation

struct Product {

private(set) public var name: String
private(set) public var imageName: String

init(name: String, imageName: String) {
    self.name = name
    self.imageName = imageName
}

}

数据模型

import Foundation

class DataServices {

static var instance = DataServices()
var productIndex = 0 

private(set) public var categories = [
    Category(imageName: "backpackingBG", title: "BACKPACKING"),
    Category(imageName: "campingBG", title: "CAMPING"),
    Category(imageName: "divingBG", title: "DIVING"),
    Category(imageName: "fishingBG", title: "FISHING"),
    Category(imageName: "hikingBG", title: "HIKING"),
    Category(imageName: "rvBG", title: "RV LIFE")
]

func getCategories() -> [Category] {
    return categories
}

private let products = [
    Product(name: "SLEEPING BAG", imageName: "sleepingBag"),
    Product(name: "CAMPING STOVE", imageName: "campingStove"),
    Product(name: "FOOD COOLER", imageName: "foodCooler"),
    Product(name: "PARACORD BRACELET", imageName: "paracordBracelet"),
    Product(name: "PUP TENT", imageName: "pupTent"),
    Product(name: "TACTICAL KNIFE", imageName: "tacticalKnife")
]

func getProducts() -> [Product] {
    return products
}

}

你必须实施

optional func collectionView(_ collectionView: UICollectionView, 
         didSelectItemAt indexPath: IndexPath)
{
  self. performSegue(withIdentifier: "ProductDetailVC", sender: indexPath.row)

}

//

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if segue.identifier == "ProductDetailVC" {
    var productDetailVC = segue.destination as! ProductDetailVC
    let index = sender as! Int
    let product = DataServices.instance.getProducts()[index]
    productDetailVC.product = product
 }
}

在您的 ProductDetailVC 中,永远不会调用 updateView() 方法。你应该在 viewdidLoad 中调用它。 ;)

class ProductDetailVC: UIViewController {

@IBOutlet weak var productName: UILabel!
@IBOutlet weak var productImageView: UIImageView!

var product: Product?

override func viewDidLoad() {
    super.viewDidLoad()
    updateView() // <-- This is what you missed!
}

func updateView() {
    productName.text = product?.name
    productImageView.image = UIImage(named: (product?.imageName)!)
}


}

此外,如果您的 segue 从未被触发,请确保在故事板中将其设置为从单元格模板到 ProductDetailVC,并确保故事板中的 segue 标识符设置为 ProductDetailVC(区分大小写)。

最后,在您的 prepareForSegue 方法中,您没有使用最后一个变量(让 product = ...),您应该将它传递给 productDetailVC 。

干杯,

您忘记通过 product,为了通过 product

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ProductDetailVC" {
        var productDetailVC = segue.destination as! ProductDetailVC
        let cell = sender as! UICollectionViewCell
        let indexPath = collectionView.indexPath(for: cell)
        let product = DataServices.instance.getProducts()[(indexPath?.row)!]
        productDetailVC.product = product
    }
}

并在 ProductDetailVC 更新 UI

override func viewDidLoad() {
    super.viewDidLoad()
    self.updateView()
}