我是一名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
}
}
答案 0 :(得分:2)
您必须实施
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
}
}
答案 1 :(得分:2)
您忘了传递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
更新用户界面
override func viewDidLoad() {
super.viewDidLoad()
self.updateView()
}
答案 2 :(得分:1)
在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方法中,您没有使用上一个变量(let product = ...),您应该将它传递给productDetailVC。
干杯,