0

UITableViewRowAction: edit and delete で UITableView を作成しました。削除を押すと、確認が UIAlertController として表示されます。ユーザーが削除を押すとセルが削除され、ユーザーがキャンセルを押すとセルが削除され、UIAlertController が消えます。このコードを書きましたが、意図したとおりに動作しません。

let edit = UITableViewRowAction(style: .normal, title: "Изменить") { action, index in
        self.performSegue(withIdentifier: "LEDChangesSegue", sender: self.tableView.cellForRow(at: indexPath))
    }
    edit.backgroundColor = UIColor.init(red: 0, green: 122.0/256.0, blue: 1, alpha: 1)

    let delete = UITableViewRowAction(style: .normal, title: "Удалить") { action, index in

        let alertController = UIAlertController(title: self.ledControllers[indexPath.row].name, message: "Действительно удалить этот контроллер?", preferredStyle: .alert)
        var answer = true

        let cancelAction = UIAlertAction(title: "Отмена", style: .cancel) { (action:UIAlertAction!) in answer = false
        }

        let deleteAction = UIAlertAction(title: "Удалить", style: .destructive ) { (action:UIAlertAction!) in answer = true
        }
        alertController.addAction(deleteAction)
        alertController.addAction(cancelAction)
        self.present(alertController, animated: true, completion: nil)

        if  answer {
            self.ledControllers.remove(at: index.row)
            saveLED(self.ledControllers)
            self.tableView.deleteRows(at: [index], with: .fade)
        }}
    delete.backgroundColor = UIColor.red()

    return [edit, delete]

var answer = trueUIAlertController でクリックしたものではなく、常に元の値を返します。正しく動作するようにコードを修正するにはどうすればよいですか?

4

1 に答える 1

0

解決策 1

answerこれはローカル変数であり、このブロックの外にスコープがないため 、変数を取得せずに直接行うことができます

let edit = UITableViewRowAction(style: .normal, title: "Изменить") { action, index in
    self.performSegue(withIdentifier: "LEDChangesSegue", sender: self.tableView.cellForRow(at: indexPath))
}
edit.backgroundColor = UIColor.init(red: 0, green: 122.0/256.0, blue: 1, alpha: 1)

let delete = UITableViewRowAction(style: .normal, title: "Удалить") { action, index in

    let alertController = UIAlertController(title: self.ledControllers[indexPath.row].name, message: "Действительно удалить этот контроллер?", preferredStyle: .alert)

    let cancelAction = UIAlertAction(title: "Отмена", style: .cancel) { (action:UIAlertAction!) in
    }

    let deleteAction = UIAlertAction(title: "Удалить", style: .destructive ) { (action:UIAlertAction!) in 
        self.ledControllers.remove(at: index.row)
        self.saveLED(self.ledControllers)
        self.tableView.deleteRows(at: [index], with: .fade)
    }
    alertController.addAction(deleteAction)
    alertController.addAction(cancelAction)
    self.present(alertController, animated: true, completion: nil)

delete.backgroundColor = UIColor.red()

return [edit, delete]

解決策 2

本当に変数でこれを行いたい場合answer(実際には使用する意味がありません)、別の関数を記述して行を削除します。

func deleteRowNow(answer : Bool, forRow : NSIndexPath)
{
    if  answer {
        self.ledControllers.remove(at: index.row)
        saveLED(self.ledControllers)
        self.tableView.deleteRows(at: [index], with: .fade)
    }}
}

のブロック内の呼び出しから呼び出しますUIAlertAction

let edit = UITableViewRowAction(style: .normal, title: "Изменить") { action, index in
    self.performSegue(withIdentifier: "LEDChangesSegue", sender: self.tableView.cellForRow(at: indexPath))
}
edit.backgroundColor = UIColor.init(red: 0, green: 122.0/256.0, blue: 1, alpha: 1)

let delete = UITableViewRowAction(style: .normal, title: "Удалить") { action, index in

    let alertController = UIAlertController(title: self.ledControllers[indexPath.row].name, message: "Действительно удалить этот контроллер?", preferredStyle: .alert)
    var answer = true

    let cancelAction = UIAlertAction(title: "Отмена", style: .cancel) { (action:UIAlertAction!) in 
          answer = false
          self.deleteRowNow(answer, forRow : index)
    }

    let deleteAction = UIAlertAction(title: "Удалить", style: .destructive ) { (action:UIAlertAction!) in 
          answer = true
          self.deleteRowNow(answer, forRow : index)
    }
    alertController.addAction(deleteAction)
    alertController.addAction(cancelAction)
    self.present(alertController, animated: true, completion: nil)

delete.backgroundColor = UIColor.red()

return [edit, delete]
于 2016-06-30T16:56:44.313 に答える