IndexPath 和 indexPath 不起作用

IndexPath and indexPath don't work

当有人试图在我的应用程序中删除俱乐部时,我正在尝试添加 AlertView。

func createAlert() {
            let sheet = UIAlertController(title: "Diesen Verein löschen?", message: "Wollen Sie diesen Verein für immer löschen?", preferredStyle:.alert)
            present(sheet, animated: true, completion: nil)
            let action1 = UIAlertAction(title: "Löschen", style: .default , handler: { (action) in
                self.deleteClub(at: IndexPath)
            })
            let action2 = UIAlertAction(title: "Abbrechen", style: .default, handler: { (action) in
                self.dismiss(animated: true, completion: nil)
            })
        }

但我不能这样做,因为带有 self.deleteClub(at: IndexPath) 的行会引发错误。我也不能用 indexPath 做到这一点。 找了好久都没找到。

这是我的 deleteClub 函数:

  func deleteClub(at indexPath: IndexPath){
            let club = clubs[indexPath.row]

            guard let managedContext = club.managedObjectContext else {
                return
            }
            managedContext.delete(club)

            do {
                try managedContext.save()

                clubs.remove(at: indexPath.row)

                tableViewClub.deleteRows(at: [indexPath], with: .automatic)
            } catch {
                print("Could not delete")

                tableViewClub.reloadRows(at: [indexPath], with: .automatic)
            }
        }

您是否尝试在 indexPath (indexPath.row) 之后添加 .row

像这样更改 createAlert 方法的定义:

func createAlert(_ indexPath: IndexPath) {
    let sheet = UIAlertController(title: "Diesen Verein löschen?", message: "Wollen Sie diesen Verein für immer löschen?", preferredStyle: .alert)

    let action1 = UIAlertAction(title: "Löschen", style: .destructive , handler: { (action) in
        self.deleteClub(at: indexPath)
    })
    let action2 = UIAlertAction(title: "Abbrechen", style: .cancel, handler: nil)
    sheet.add(action1)
    sheet.add(action2)
    present(sheet, animated: true, completion: nil)
}

a) 将操作添加到警报和 b) 从 "Löschen"行动。

当然,您随后必须将 table 或集合单元格的 indexPath 传递给此方法。