![](/img/trans.png)
[英]How to get the indexpath of a custom table view cell when an UIImage inside of it is tapped and captured by UITapGestureRecognizer in prepareForSegue
[英]Accessing a Table View IndexPath from prepareForSegue
在我的表視圖控制器中的prepare segue函數中,索引Path似乎出現錯誤。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showView"){
let detailVC: DetailViewController = segue.destinationViewController as! DetailViewController
let indexPath = self.MainTableView.indexPathForSelectedRow!
detailVC.test_label.text = names[indexPath.row]
self.MainTableView.deselectRowAtIndexPath(indexPath, animated:
}
}
對於indexPath.row,它說“ BAD_INSTRUCTION”。 當我刪除索引名稱數組的行時,它將在上面的行(indexPath定義行)上引發BAD INSTRUCTION錯誤。 當我改變了! indexPathForSelectedRow到()之后,顯示“不是函數”。 我無法弄清楚該如何為所選行獲取正確的索引路徑! 有人知道發生了什么嗎?
-控制台中的錯誤是:致命錯誤:解開可選值時意外發現nil
在let indexPath = ...
行上放置一個斷點。 查看MainTableView
對象是否為nil,因為您可能因為強制展開nil對象而崩潰了。 如果為零,請檢查您與情節提要/ xib文件的連接。
Here please find this code. This should work fine.
還是換! 至 ? 在您的代碼中。 //讓indexPath = self.MainTableView.indexPathForSelectedRow?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if(segue.identifier == "showView")
{
let detailVC = segue.destinationViewController as! ViewController2
let path = tableView.indexPathForSelectedRow?.row
detailVC.test_label.text = names[path]
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.