override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case toProductIdentifier: let destination = segue.destination as? ProductViewController
destination?.configureProduct(for: products[selectedRowIndex.row])
default: break
}
}
所以我在行目的地收到此錯誤?.configureProduct(for: products[selectedRowIndex.row])
這是 func configureProduct 的代碼,但我真的不認為這個 func 有這個問題
func configureProduct(for product: instockProduct) {
DispatchQueue.main.async {
let brand = product.brand["name"] as? String ?? ""
self.productBrandLabel.text = brand.uppercased()
self.productNameLabel.text = product.name
self.productPriceLabel.text = product.price.stringValue
}
}
uj5u.com熱心網友回復:
您正在indexPath使用空值進行實體化indexPath = IndexPath(),并且可能沒有使用indexPathtableView 提供給您的值,因此會出現錯誤。我在 Playground 中用簡化的示例復制了您的代碼并得到了同樣的錯誤。
嘗試檢查該didSelectItemAt函式是否被正確觸發,并且您真正將selectedRowIndexindexPath 從回傳的變數分配給變數,UITableView而不是使用預先創建的IndexPath()值。

uj5u.com熱心網友回復:
問題是在prepare(for segue.
您需要一個臨時屬性ProductViewController并在其中配置插座viewDidLoad
var product : Product! // replace Product with the real type
func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
let brand = product.brand["name"] as? String ?? ""
self.productBrandLabel.text = brand.uppercased()
self.productNameLabel.text = product.name
self.productPriceLabel.text = product.price.stringValue
}
}
并替換prepare(for segue為
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case toProductIdentifier:
let destination = segue.destination as! ProductViewController
let product = sender as! Product
destination.product = product
default: break
}
}
該product專案在sender引數中傳遞。洗掉selectedRowIndex并替換didSelectItem為
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let product = products[indexPath.row]
print(product.name)
collectionView.deselectItem(at: indexPath, animated: true)
self.performSegue(withIdentifier: toProductIdentifier, sender: product)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/408330.html
標籤:
上一篇:iOS自定義字體不添加到系統
下一篇:如何擺脫未定義預編譯器定義的警告
