我目前正在學習Swift,並嘗試在用戶點擊應用程序呈現的其中一個tableview單元格時執行segue。目前,無論用戶何時執行此操作,下一個視圖控制器都會成功加載,但似乎出於某種原因,我無法訪問其任何UI元素,因爲每次我嘗試這樣做時,我都會結束收到此錯誤:在tableview單元格上執行segue時出現問題
致命錯誤:意外發現零而展開的可選值
錯誤指向,我嘗試修改顯示在一個標籤的文本行下一個視圖控制器
這是didSelectRowAt功能:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
self.performSegue(withIdentifier: "segue1", sender: self)
}
,這是prepareForSegue功能:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue1" {
let destinationVC = segue.destination as! UserViewController
let selectedRow = tableView.indexPathForSelectedRow!
let selectedCell = tableView.cellForRow(at: selectedRow) as! CustomCell
destinationVC.usernameLabel.text = selectedCell.userName.text //this is where the error is pointing to
destinationVC.bioLabel.text = selectedCell.bio.text
destinationVC.userImage.image = selectedCell.photo.image
}
}
我不知道是什麼原因造成這個問題的想法。我的目標是將數據從竊聽單元傳遞到下一個視圖控制器,但顯然這阻止了我這樣做。有誰知道我該如何解決這個問題?提前致謝。
嗨,邁克。你在使用靜態或動態的'UITableView'單元嗎? –
我正在使用動態單元格。 –
@Mike Vaugh,使用'destinationVC.usernameLabel.text = selectedCell.userName.text ?? 「」' – aircraft