簡體   English   中英

UITableViewRowAction和UIAlertController

[英]UITableViewRowAction and UIAlertController

我用UITableViewRowAction創建了UITableView:編輯和刪除。 當按下刪除鍵時,確認應顯示為UIAlertController。 如果用戶按Delete鍵,則應刪除單元格,如果用戶按Cancel鍵,則應刪除單元格,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 = true ,而不是我在UIAlertController中單擊的內容。 什么修復了代碼以使其正常工作?

解決方案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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM