ios - UITableViewRowAction 和 UIAlertController

标签 ios xcode swift uialertcontroller uitableviewrowaction

我已经使用 UITableViewRowAction 创建了 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 = true ,而不是我在 UIAlertController 中单击的值。如何修复代码以使其正常工作?

最佳答案

解决方案1

您可以直接执行此操作,而无需使用变量 answer,因为这是局部变量,并且在此 block 之外没有作用域

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 block 内的调用中调用它

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]

关于ios - UITableViewRowAction 和 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38120459/

相关文章:

android - 需要为 iOS 和 Android 实现即时消息?

objective-c - 为报亭应用制作应用内商店

ios - iOS 中的 Google Admob 集成错误

ios - iOS 8 上 UITableView 中的标签重叠,但 iOS 7 上不重叠

ios - 无法将文件保存在 tmp 目录中

ios - UIVisualEffectView 延迟与 UIModalTransitionStyle CrossDissolve

iphone - 将图像命名为导航栏名称

swift spritekit : How to stop a runBlock?

ios - 如何使用 swift 在 xcode 中删除(或不使用)启动屏幕 Storyboard

android - 直接从浏览器(或从应用程序)发送到 whatsapp 组的链接