【问题标题】:Hiding and showing section in tableview upon a button request根据按钮请求在表格视图中隐藏和显示部分
【发布时间】:2017-05-15 08:03:28
【问题描述】:

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

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

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

【问题讨论】:

  • 您预计会发生什么,实际会发生什么?
  • @Abizern:最初,当应用程序加载该部分时,该部分是隐藏的,并且上面有一个“启用部分”按钮当我单击按钮并输入管理员密码时,我希望在正确的密码下该部分变为可用并且“启用部分”按钮变为隐藏状态。目前使用代码发生的情况是,如果我输入正确的密码,则什么也不会发生(这是我需要帮助来显示该部分并隐藏按钮的地方)并且如果用户输入了错误的管理员密码,它会发出警告说“密码错误"
  • 如果用户成功登录,我在您的部分代码中看不到任何更改显示内容的内容。
  • @Abizern:下面是用户成功登录的部分。我需要这部分的帮助。如何使该部分可见并隐藏按钮? if let x = UserDefaults.standard.string(forKey: "initial admin password"), x == field { }

标签: swift uitableview button uialertcontroller sections


【解决方案1】:

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

1.假设你有一个标志:

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

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

【讨论】:

  • 确实如此。感谢您的帮助。
猜你喜欢
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多