swift - 根据按钮请求在表格 View 中隐藏和显示部分

标签 swift uitableview button uialertcontroller sections

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

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

最初将第 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.假设你有一面旗帜:

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

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

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

关于swift - 根据按钮请求在表格 View 中隐藏和显示部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43974240/

相关文章:

Java/SWT/如何从按钮中删除框架

Swift 协议(protocol)要求属性作为协议(protocol)

ios - 如何在 Collection View 中的集合可重用 View 下添加元素

ios - 如何让用户修改 UITableView 单元格中的文本

ios6 - 调用 beginRefreshing 且 contentOffset 为 0 时,UIRefreshControl 不显示刺状

ios - 单一选择在 UITableview 中不起作用

button - Twitter Bootstrap 按钮切换 - 如何使开/关状态颜色对比更明显

C程序按钮长按持续计数

ios - 从 Collection 1 更新 Collection 中的单元格内容

ios - 为什么prepareForSegue不工作