swift 如何在prepare(for:segue)

utugiqy6  于 2023-03-22  发布在  Swift
关注(0)|答案(1)|浏览(150)

我正在为一个项目使用SwipeCellKit SwipeCellKit,我想让它滑动以显示一个编辑按钮,然后单击编辑以转到另一个视图控制器。
当我点击该行时,它工作了,将Auto对象传递给新的视图控制器,使用以下代码:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print (indexPath.row)
    performSegue(withIdentifier: "editAutoDetailsSegue", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "editAutoDetailsSegue" {
        print("Preparing to segue")
        let destinationVC = segue.destination as! EditAutoInfoViewController
        if let indexPath = tableView.indexPathForSelectedRow {
            print("Row Selected")
            destinationVC.selectedAuto = autombiles?[indexPath.row]
        }
    }
}

下面是我实现的扩展。它可以按原样工作,但是当我执行segue时,它不会将Auto对象传递给新的视图控制器。

extension AutoViewController: SwipeTableViewCellDelegate {
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        guard orientation == .right else { return nil }

        let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
            // handle action by updating model with deletion
            if let theAuto = self.autombiles?[indexPath.row] {
                do {
                    try self.realm.write {
                        self.realm.delete(theAuto)
                    }
                } catch {
                    print("There was an error deleting the auto \(error)")
                }
            }
        }
        
        let editAction = SwipeAction(style: .default, title: "Edit") { action, indexPath in
            // Go to Editor
            self.performSegue(withIdentifier: "editAutoDetailsSegue", sender: self)
        }

        return [deleteAction, editAction]
    }
    
    func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
        var options = SwipeOptions()
        options.expansionStyle = .destructive
        options.transitionStyle = .border
        return options
    }
}

有人知道如何从被刷的单元格访问索引路径吗?

u3r8eeie

u3r8eeie1#

感谢Paulw 11的提示-这只是解决方案的一部分,但它是我遗漏的至关重要的一部分。
所以我设置了sender:indexPath,但我也不得不告诉准备:接下来...怎么处理。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "editAutoDetailsSegue" {
        let destinationVC = segue.destination as! EditAutoInfoViewController
        if let indexPath = tableView.indexPathForSelectedRow {
            destinationVC.selectedAuto = autombiles?[indexPath.row]
        }
        if let indexPath = sender as? NSIndexPath {
            destinationVC.selectedAuto = autombiles?[indexPath.row]
        }
    }
}

还有

let editAction = SwipeAction(style: .default, title: "Edit") { action, indexPath in
        // Go to Editor
        self.performSegue(withIdentifier: "editAutoDetailsSegue", sender: indexPath)
    }

非常感谢!

相关问题