[英]Swift - Open ViewController on Tapping a TableViewCell
[英]TableViewCell to ViewController - Swift
我嘗試了很多方法,但仍然無法轉到另一個ViewController
。 這是我的故事板。
這是我的代碼。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? HistoryMainTableViewCell {
listItems.sort() { $0.seq > $1.seq }
let history = listItems[indexPath.row]
cell.setValue(history: history)
return cell
} else {
return HistoryMainTableViewCell()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "vcProductHistory" {
if let productHistoryPage = segue.destination as? HistoryProductViewController {
if let IndexPath = tableViewHistory.indexPathForSelectedRow {
let historyArray = listItems[IndexPath.row]
productHistoryPage.history = historyArray
}
}
}
}
嘗試以下代碼:( 未經測試的代碼 )
注意:以下代碼將觸發准備segue。 何時選擇tableViewCell,讓我知道代碼的工作原理...
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
// let indexPath = myTableView!.indexPathForSelectedRow
// let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
//sendSelectedData = (currentCell.textLabel?.text)! as String as NSString
performSegue(withIdentifier: "vcProductHistory", sender: self)
}
根據您對我的初始評論的回答:將行從初始vc拖到目標vc會告訴您vc有關segue的信息,准備segue會告訴vc當觸發segue時該怎么做,但是您仍然需要讓vc知道何時觸發它應該執行segue。 嘗試這個:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? HistoryMainTableViewCell else { return HistoryMainTableViewCell() }
let history = listItems[indexPath.row]
cell.setValue(history: history)
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "vcProductHistory" {
guard let productHistoryPage = segue.destination as? HistoryProductViewController, let indexPath = sender as? IndexPath else { return }
let historyArray = listItems[indexPath.row]
productHistoryPage.history = historyArray
}
}
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
performSegue(withIdentifier: "vcProductHistory", sender: indexPath)
}
我認為使用防護語法會更整潔。 另外,它使用發送方作為indexPath,而不使用tableView.indexPathForSelectedRow。
另外,我注意到您的cellForRowAtIndexPath中有以下內容:
listItems.sort() { $0.seq > $1.seq }
您在cellForRowAtIndexPath中對數據源進行排序。 您不能在那里執行此操作,因為該函數將分別為每一行調用。 您需要在調用viewWillAppear之類的地方進行排序。 另外,要對數組進行排序,需要在排序后刪除()。 嘗試這個。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
listItems.sort { $0.0.seq > $0.1.seq }
tableView.reloadData()
}
我認為您應該連接到UINavigationController,而不要連接到HistoryProductionViewController
因為您的MainViewController的segue已連接到Navigation Controller
我猜在func prepareForSegue
segue.identifier == "vcProductHistory"
它可以工作,但是, if let productHistoryPage = segue.destination as? HistoryProductViewController
if let productHistoryPage = segue.destination as? HistoryProductViewController
不起作用
因為您的目標ViewController的類是NavigationController
你應該打電話
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "vcProductHistory" {
if let naviVC = segue.destination as? UINavigationController {
if let productHistoryPage = naviVC.topViewController as? HistoryProductViewController {
if let IndexPath = tableViewHistory.indexPathForSelectedRow {
let historyArray = listItems[IndexPath.row]
productHistoryPage.history = historyArray
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.