請幫助我理解為什么 tableView.indexPathForSelectedRow 方法回傳 nil。
我想從表視圖控制器轉移到視圖控制器。我有一個故事板和leadingSwipeActions 的轉場。
import UIKit
class TableViewController: UITableViewController {
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return months.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = months[indexPath.row]
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editItem" {
let path = tableView.indexPathForSelectedRow
print("\(path)") ##// Prints nil.##
}
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let editAction = UIContextualAction(style: .normal, title: "Edit Item") { [self] (action, view, completion) in
performSegue(withIdentifier: "editItem", sender: self)
}
editAction.backgroundColor = .systemOrange
return UISwipeActionsConfiguration(actions: [editAction])
}
}
uj5u.com熱心網友回復:
前導滑動(以及隨后輕擊操作按鈕)不會選擇行。
由于sender“prepare for segue”方法的引數定義為,一種方法是在呼叫 segue 時Any?傳遞:indexPath
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editItem" {
// make sure an Index Path was passed as the sender
if let path = sender as? IndexPath {
print("Index Path: \(path)")
}
}
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
// note: use [weak self] to avoid possible retain cycle
let editAction = UIContextualAction(style: .normal, title: "Edit Item") { [weak self] (action, view, completion) in
guard let self = self else { return }
// pass the path as the sender
self.performSegue(withIdentifier: "editItem", sender: indexPath)
}
editAction.backgroundColor = .systemOrange
return UISwipeActionsConfiguration(actions: [editAction])
}
編輯-回應評論
該sender: Any?引數意味著sender可以是任何物件。這使您可以區分啟動 segue 的操作或代碼段。
例子:
self.performSegue(withIdentifier: "editItem", sender: indexPath)
self.performSegue(withIdentifier: "editItem", sender: 1)
self.performSegue(withIdentifier: "editItem", sender: 2)
self.performSegue(withIdentifier: "editItem", sender: "Hello")
self.performSegue(withIdentifier: "editItem", sender: self)
self.performSegue(withIdentifier: "editItem", sender: myButton)
然后,在 中prepare(...),您可以評估sender以決定下一步該做什么。
很多時候,當使用表格視圖“推送”到與點擊的單元格相關的另一個控制器(例如,“詳細資訊”控制器)時,開發人員會將單元格原型的轉場連接到詳細資訊視圖控制器。然后(如果你給那個 segue 一個識別符號“fromCell”),你可以做這樣的事情:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "fromCell" {
if let cell = sender as? UITableViewCell {
if let path = tableView.indexPath(for: cell) {
print("Index Path: \(path)")
// here we might get data for that row/section,
// and pass it to the next controller
}
}
}
}
在您的情況下,我們發送indexPath我們剛剛操作的單元格作為sender引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505321.html
上一篇:如何延遲運行一行代碼,直到影片完成?(調度組和調度佇列)
下一篇:HTML核心標簽和屬性
