你應該做的是通過prepareForSegue:sender:方法傳遞所需的數據。你可以通過執行實現這一目標以下:
1-聲明selectedCell
作爲一個實例變量是在整個視圖控制器可訪問的:從
// for sure you'll need to declare its data type...
var selectedCell:...
2-刪除「傳遞數據」的代碼tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
委託方法,所有你所要做的就是執行SEGUE:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedCell = listAthkar[indexPath.row]
performSegue(withIdentifier: "showCell", sender: self)
}
3-通過它實現prepareForSegue:sender:
並傳遞數據:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// you may need to check the segue identifier in case of your view controller contains multiple segues
if segue.identifier == "showCell" {
let destinationVC = segue.destination as! showCellVC()
destinationVC.cellTitle.text = selectedCell.title
destinationVC.cellDisc.text = selectedCell.details
}
}
通常,最終的結果應該是類似:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// STEP #01:
// for sure you'll need to declare its data type...
var selectedCell:...
// STEP #02:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedCell = listAthkar[indexPath.row]
performSegue(withIdentifier: "showCell", sender: self)
}
// STEP #03:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// you may need to check the segue identifier in case of your view controller contains multiple segues
if segue.identifier == "showCell" {
let destinationVC = segue.destination as! showCellVC()
destinationVC.cellTitle.text = selectedCell.title
destinationVC.cellDisc.text = selectedCell.details
}
}
}
https://stackoverflow.com/questions/32170456 /什麼 - 致命錯誤 - 意外發現 - 零 - 解包 - 可選 - 價值https://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-控制器和其他對象在迅速。歡迎來到SO。哈立德,請確保您在提問之前先搜索線索。如果沒有幫助,您可以發佈問題並告訴我們爲什麼解決方案不起作用,並且幫助您更容易,而不是發佈重複內容。 :) GL。 – 2017-08-27 07:00:20
你必須在'prepareForSegue'方法中傳遞數據,而不是'tableView:didSelectRow',顯示'prepareForSegue'方法 –