Swift 根据按钮请求在tableview中隐藏和显示节

Swift 根据按钮请求在tableview中隐藏和显示节,swift,uitableview,button,uialertcontroller,sections,Swift,Uitableview,Button,Uialertcontroller,Sections,我有一个具有不同部分的tableview。加载视图后,我隐藏该部分,并在该部分上创建一个覆盖按钮,单击该按钮后,将显示一个询问管理员密码的警报框 问题:现在,我试图显示用户输入正确密码并隐藏按钮后最初隐藏的部分需要这方面的帮助 最初将节1设置为隐藏并创建覆盖按钮: override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section ==

我有一个具有不同部分的tableview。加载视图后,我隐藏该部分,并在该部分上创建一个覆盖按钮,单击该按钮后,将显示一个询问管理员密码的警报框

问题:现在,我试图显示用户输入正确密码并隐藏按钮后最初隐藏的部分需要这方面的帮助

最初将节1设置为隐藏并创建覆盖按钮:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section == 1 {

        let enableButton = UIButton(frame: CGRect(origin: CGPoint(x: 320, y: 160), size: CGSize(width: 130, height: 30)))
        enableButton.backgroundColor = UIColor.clear
        enableButton.setTitle("Enable Section", for: .normal)
        enableButton.setTitleColor(.blue, for: .normal)
        enableButton.addTarget(self, action: #selector(ConfigTableViewController.enableButtonClicked), for: .touchUpInside)
        self.view.addSubview(enableButton)

        return 0
    }

    else if section == 2 {
        return 2
    }
    else if section == 3 {
        return 2
    }

    return 1
}
单击按钮时,此func调用:

func enableButtonClicked() {

    let alertController = UIAlertController(title: "Admin Password", message: "Please input admin password", preferredStyle: .alert)

    let enable = UIAlertAction(title: "Enable", style: .default) { (_) in
        let field = alertController.textFields?[0].text
        if let x = UserDefaults.standard.string(forKey: "initial admin password"), x == field {


        }
        else{

            let wrongPwd = UIAlertController(title: "Wrong Admin Password", message: nil, preferredStyle:UIAlertControllerStyle.alert)
            wrongPwd.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
            self.present(wrongPwd, animated: true, completion: nil)
        }
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

    alertController.addTextField { (textField) in
        textField.placeholder = "Admin Password"
        textField.isSecureTextEntry = true
    }

    alertController.addAction(enable)
    alertController.addAction(cancelAction)

    self.present(alertController, animated: true, completion: nil)
}

为了解决您的问题,我建议您创建一个
标志
,检查您的
密码是否正确

1.假设您有一面旗帜:

var correctPasswordFlag : Bool = false // initially assume it wrong
  • 只需将一个按钮拖动到您的单元格中,创建
    IBOutlet
    say
    configEnableButton
    ,然后将目标选择器添加到此方法:

    func enableButtonClicked(){


  • 希望这能帮助您解决问题。享受编码并不断学习。

    您预计会发生什么,实际会发生什么?@Abizern:最初,当应用程序加载时,分区会隐藏,并在其上方有一个按钮“启用分区”当我点击按钮并输入管理员密码时,我希望在正确的密码下,该部分变为可用,“启用部分”按钮变为隐藏。目前,代码中发生的情况是,如果我输入正确的密码,什么也不会发生(这是我需要帮助来显示该部分并隐藏按钮的地方)如果用户输入了错误的管理员密码,它会发出警告说“密码错误”我在您的分区代码中看不到任何更改用户成功登录时显示内容的内容。@Abizern:下面是用户成功登录的部分。我需要这部分的帮助。如何使分区可见并隐藏按钮?如果让x=UserDefaults.standard.string(forKey:“初始管理员密码”),x==字段{}确实如此。谢谢您的帮助。
        let alertController = UIAlertController(title: "Admin Password", message: "Please input admin password", preferredStyle: .alert)
    
        let enable = UIAlertAction(title: "Enable", style: .default) { (_) in
            let field = alertController.textFields?[0].text
            if let x = UserDefaults.standard.string(forKey: "initial admin password"), x == field {
               //For correct password
               correctPasswordFlag  = true
               //Reload Tableview
                configTableview.relaod() // if not then create iboutlet of tableview
            }
            else{
              //For wrong password
               correctPasswordFlag  = false
               //Reload Tableview
                configTableview.relaod() // if not then create iboutlet of tableview
                let wrongPwd = UIAlertController(title: "Wrong Admin Password", message: nil, preferredStyle:UIAlertControllerStyle.alert)
                wrongPwd.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
                self.present(wrongPwd, animated: true, completion: nil)
            }
        }
    
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
    
        alertController.addTextField { (textField) in
            textField.placeholder = "Admin Password"
            textField.isSecureTextEntry = true
        }
    
        alertController.addAction(enable)
        alertController.addAction(cancelAction)
    
        self.present(alertController, animated: true, completion: nil)
    
     }