简体   繁体   English

如何在集合视图单元格Swift中检测所有OTP文本字段是否已填充

[英]How to detect all OTP textfields are filled or not in Collection view cell Swift

I have a screen called OTP.我有一个名为 OTP 的屏幕。 In that, I have set dynamic textfields using collectionview cell.在那,我使用collectionview单元设置了动态文本字段。 I am able to fetch the user enters text from that textfields using the tag.我能够使用标签获取用户从该文本字段中输入的文本。 But, I have an issue like, All four textfields filled then only I have to enable the Send button in the UI until that Send button would be disabled.但是,我有一个问题,例如,所有四个文本字段都已填满,然后我只需要在 UI 中启用“发送”按钮,直到该“发送”按钮被禁用。

I am able to get text from textfields based on the textfield tag.我能够根据文本字段标签从文本字段中获取文本。 But, How to detect wether all textfields filled or not from collectionview cell?但是,如何从 collectionview 单元格中检测是否所有文本字段都已填充?

Any suggestions?有什么建议么?

My code is below:我的代码如下:

 class OTPViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate {

    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return otpTextCount //either 4 or 6 number comes from server array
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! OTPCollectionViewCell
        cell.otpTextField.tag = indexPath.row
        cell.otpTextField.delegate = self
        return cell
    }
    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        textField.text = textField.text?.replacingOccurrences(of: " ", with: "")
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        var cell: OTPCollectionViewCell?
        cell = textField.superview?.superview as? OTPCollectionViewCell
        //        print(cell?.otpTextField.text as! String)
        //        print(cell?.otpTextField.tag)
    }

I would recommend you to store text that were inserted into UITextFields into some sort of dictionary [Int: String] where key is the tag of cell.我建议您将插入 UITextFields 的文本存储到某种字典[Int: String]中,其中键是单元格的标记。 Then if the dictionary contains all necessary tags we can enable the button.然后,如果字典包含所有必要的标签,我们可以启用该按钮。

The example follows.示例如下。 I personally would recommend storing states in something like a ViewModel and moving that logic there.我个人建议将状态存储在ViewModel类的东西中,并将该逻辑移到那里。 But this is a sample code that I did not compile, its purpose it to simply show the idea.但这是一个我没有编译的示例代码,它的目的是简单地展示这个想法。

class OTPViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate {
  var insertedValues = [Int: String]()

  func numberOfSections(in collectionView: UICollectionView) -> Int {
      return 1
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
      return otpTextCount //either 4 or 6 number comes from server array
  }

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! OTPCollectionViewCell
      cell.otpTextField.tag = indexPath.row
      cell.otpTextField.delegate = self
      return cell
  }

  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
      textField.text = textField.text?.replacingOccurrences(of: " ", with: "")
      return true
  }

  func textFieldDidEndEditing(_ textField: UITextField) {
    
      guard let cell = textField.superview?.superview as? OTPCollectionViewCell else {
          assertionFailure("Orphan textField did end editing. No cell found")
          return
      } 

      if textField.text == "" {
          insertedValues[cell.tag] = nil
      } else {
          insertedValues[cell.tag] = textField.text
      }

      theButton.isEnabled = areAllFieldsFilled()
      //        print(cell?.otpTextField.text as! String)
      //        print(cell?.otpTextField.tag)
  }

  func areAllFieldsFilled() -> Bool {
      for n in 0..<otpTextCount {
          if !insertedValues.keys.contains(n) {
             return false
          }
      }

      return true
  }

I have found following way and it worked fine.我找到了以下方法,并且效果很好。

    private var arrayOfCells: [OTPCollectionViewCell] = []
    var insertedValues = [String]()

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! OTPCollectionViewCell
        cell.otpTextField.tag = indexPath.row
        cell.otpTextField.delegate = self
        arrayOfCells += [cell]
        return cell
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        
        if insertedValues.count > 0 {
            insertedValues.removeAll()
        }
        
        for i in 0..<arrayOfCells.count {
            if let textfieldText = arrayOfCells[i].otpTextField.text, textfieldText != "" {
                insertedValues.append(textfieldText)
                if insertedValues.count == 4 //otpTextCount{
                    nextButton.isUserInteractionEnabled = true
                }
            } else {
                nextButton.isUserInteractionEnabled = false
                
            }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM