私は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
}
}
パスの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()
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加