2017-05-15 8 views
1

다른 섹션이있는 테이블 뷰가 있습니다. 보기를로드 할 때 섹션을 숨기고 섹션 위에 오버레이 버튼을 만들면 클릭하면 관리자 비밀번호를 묻는 경고 상자가 표시됩니다.단추 요청시 tableview의 섹션 숨기기 및 표시

문제 2 : 이제 숨겨진 섹션을 표시하려고합니다. 사용자가 올바른 비밀번호를 입력하고 버튼을 숨기면 본

는 처음에 숨길 수있는 섹션 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) 
} 
+0

실제로 어떤 일이 발생할 것으로 예상됩니까? – Abizern

+0

@Abizern : 처음에 앱이 섹션을 숨기고 버튼 위에 "Enable section"버튼을 클릭하고 관리자 비밀번호를 입력하면 섹션이 사용 가능하게되고 "섹션 사용" 버튼이 숨겨집니다. 현재 코드를 사용하면 올바른 비밀번호를 입력해도 아무 것도 나타나지 않습니다 (이 섹션을 표시하고 버튼을 숨기는 데 도움이 필요합니다). 사용자가 잘못된 관리자 비밀번호를 입력하면 '비밀번호가 틀립니다. " – habed

+0

사용자가 성공적으로 로그인하면 표시되는 내용을 변경하는 섹션 코드에는 아무것도 표시되지 않습니다. – Abizern

답변

1

가 문제를 해결하기 위해 , PassWord이 정확한지 여부를 확인하는 flag을 만들 것을 제안합니다.

당신은 플래그가 1.Suppose :

:

var correctPasswordFlag : Bool = false // initially assume it wrong 
  • 단순히 셀에 버튼을 드래그 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) 
      
      } 
      

    호프이 문제를 해결하는 데 도움이됩니다. 코딩을 즐기고 학습을 계속하십시오.

  • +1

    그랬습니다. 당신의 도움을 주셔서 감사합니다. – habed