Search code examples
swiftuitableviewbuttonuialertcontrollersections

Hiding and showing section in tableview upon a button request


I have a tableview that has different sections. Upon loading the view I hide the section and create a overlay button over the section which upon clicking it it displays a alert box asking for admin password

PROBLEM: Now, I am trying to show the section that was initially hidden once the user inputs the right password and hide the button. NEED HELP WITH THIS

Initially setting the section 1 to be hidden and creating overlay button:

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
}

This func calls when the button is clicked:

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

Solution

  • To solve your problem , i will suggest you to create a flag which check whether your PassWord is correct or not.

    1.Suppose you have a flag :

    var correctPasswordFlag : Bool = false // initially assume it wrong
    
    1. Simply Drag a button to your cell and create IBOutlet say configEnableButton and add Target Selector to this Method :

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

    Hope this Helps you to fix your issue . Enjoy coding and Keep Learning.