【发布时间】:2018-03-28 10:44:59
【问题描述】:
我是一名 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
}
}
【问题讨论】:
-
在您的代码中准备 segue
productDetailVC.product = product您没有将产品传递给productDetailVC
标签: ios swift xcode uicollectionview collectionview