0
使用collectionViewCells並嘗試通過segue將數據發送到下一個ViewController。儘管獲取indexPath並不像tableViewCell那樣簡單,但我可以做這樣的事情來獲得它,但是我仍然得到了「意外地發現零,同時展開一個可選值」的錯誤。在PreparetoSegue期間檢索選定的UICollectionViewCell的索引路徑?
下面是代碼:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let cell = sender as? MainCollectionViewCell {
if let indexPath = self.collectionview.indexPath(for: cell) {
// use indexPath
print(indexPath.row)
let destvc: H1ViewController = segue.destination as! H1ViewController
destvc.backgroundimg.image = UIImage(named: magazines[indexPath.row].image)
}
}
}
我打印indexpath.row和所獲得的價值。所以我相信,就它而言,我沒有問題。我正在使用,如果讓我確保我沒有強有力的解開。這裏有什麼可能是錯的?
對於segue.destination as! H1ViewController做一個if let,這樣你就可以看到它是正確的類型。也請嘗試使用調試器。 – Gerriet
此外,這發生在運行時,肯定不是編譯器錯誤,所以我建議刪除該標記。 – Gerriet
@Gerriet剛剛嘗試使用,如果讓這條線以及。依然沒有! –