[英]not able to figure out what sender to return from a leadingSwipeActions for a prepare for segue
抱歉,我是全新的 swift 和 IOS 開發人員,但我有一個表格視圖,表格視圖中的每個元素都可以點擊該項目轉到我的編輯屏幕,該屏幕確定表格單元格中的信息。 我還希望有一個leadingSwipeActionsConfigurationForRowAt
出一個編輯按鈕,該按鈕使用與點擊單元格相同的segue 方式進入編輯屏幕。 問題是我的prepare(for segue:)
處理tap segue 的語句由於leaderSwipeAction 的發送者而一直失敗。 現在我真的很困惑我應該把什么作為我的發件人放在我的leadingSwipeAction 中。 我試過一個UITableViewCell()
失敗了這個守衛聲明:
guard let ViewController = segue.destination as? ViewController else { fatalError("Unexpected destination: \\(segue.destination)") }
因為是 UIView(),然后我嘗試發送一個taskCellTableViewCell
,它是我調用單個單元格的對象/類,但返回了一個 nil/空單元格,該單元格在准備中的另一個保護語句失敗了。 所以現在我對如何獲取和發送執行了 LeadershipSwipeAction 的實際單元格感到困惑。 我希望任何人都可以幫助我找出我需要返回以通過准備的正確發件人。 謝謝
這是用於leadingSwipeAction 和prepare 的代碼:
//leading swipe for editing code
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let edit = UIContextualAction(style: .normal, title: "Edit") { (contextualAction, view, actionPerformed: (Bool) -> ()) in
self.performSegue(withIdentifier: "showDetail", sender: UITableViewCell())
actionPerformed(true)
}
return UISwipeActionsConfiguration(actions: [edit])
}
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case "addItem":
os_log("Adding a new task.", log: OSLog.default, type: .debug)
case "showDetail":
guard let ViewController = segue.destination as? ViewController else {
fatalError("Unexpected destination: \(segue.destination)")
}
guard let selectedTaskCell = sender as? taskCellTableViewCell else {
fatalError("Unexpected sender: \(sender)")
}
guard let indexPath = tableView.indexPath(for: selectedTaskCell) else {
fatalError("The selected cell is not being displayed by the table")
}
let selectedTask = tasks[indexPath.row]
ViewController.Task = selectedTask
default:
fatalError("Unexpected Segue Identifier; \(segue.identifier)")
}
}
再次嘗試使用 showDetail segue 失敗。
我不認為tableViewCell
可以用作發件人,您是否無法將其設置為您從中啟動 segue 的ViewController
,這應該可以工作。
好的,在使用我能想到的每個流行詞搜索並更改我的谷歌搜索之后,我終於找到了答案。 要使發送方成為對發送方執行LeadingSwipeAction
的當前單元格,需要將tableView.cellForRow(at: indexPath)
設置為將tableView 傳遞給LeadingSwipe 函數並從indexPath 獲取單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.