根据按钮请求在表格视图中隐藏和显示部分

Hiding and showing section in tableview upon a button request

我有一个包含不同部分的表格视图。加载视图后,我隐藏该部分并在该部分上创建一个覆盖按钮,单击它会显示一个警告框,要求输入管理员密码

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

最初将第 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 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)
}

为了解决您的问题,我建议您创建一个 flag 来检查您的 PassWord 是否正确。

1.Suppose 你有一个标志 :

var correctPasswordFlag : Bool = false // initially assume it wrong
  1. 只需将一个按钮拖到您的单元格并创建 IBOutletconfigEnableButton 并将目标选择器添加到此方法:

    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 {
               //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)
    
     }
    

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