Swift 切换文本字段类

Swift 切换文本字段类,swift,enums,textfield,collectionview,Swift,Enums,Textfield,Collectionview,我切换了CollectionView和CollectionView单元格。首先我有这个 在我收到第二个带有代码的手机后 我使用相同的sell和不同的方法来更改文本,但在第一个cell.textField中作为自定义cocoapod库TKFormTextField,在第二种情况下,我希望使用简单的UITextField。什么枚举以及如何使用它? 这是我的手机 class PhoneNumberCollectionViewCell: UICollectionViewCell, NiBLo

我切换了CollectionView和CollectionView单元格。首先我有这个

在我收到第二个带有代码的手机后

我使用相同的sell和不同的方法来更改文本,但在第一个cell.textField中作为自定义cocoapod库TKFormTextField,在第二种情况下,我希望使用简单的UITextField。什么枚举以及如何使用它?

这是我的手机

    class PhoneNumberCollectionViewCell: UICollectionViewCell, NiBLoadable {



    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var PhoneNumberTextField: UITextField!
    @IBOutlet weak var SecurityLabel: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
        Decorator.decorate(self)

    }

    func setPhoneLabelText(text: String) {
        phoneLabel.text = text
    }

    func setSecurityLabel(text: String) {
        SecurityLabel.text = text
    }


}

    extension PhoneNumberCollectionViewCell {

        fileprivate class Decorator {
            static func decorate(_ cell: PhoneNumberCollectionViewCell) {
                cell.phoneLabel.textColor = UIColor(red: 50.0/255.0, green: 50.0/255.0, blue: 50.0/255.0, alpha: 1.0)
                cell.SecurityLabel.textColor = UIColor(red: 50.0/255.0, green: 50.0/255.0, blue: 50.0/255.0, alpha: 0.6)
                cell.phoneLabel.font = UIFont(name: "OpenSans", size: 15)
                cell.SecurityLabel.font = UIFont(name: "OpenSans", size: 12)
            }
        }
    }




    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let models = model[indexPath.row]

    switch models {
    case .phoneNumber:
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhoneNumberCollectionViewCell.name, for: indexPath) as? PhoneNumberCollectionViewCell {
            cell.PhoneNumberTextField.text = self.phoneNumber
            cell.setSecurityLabel(text: "_ALLYOURDATAISINSECUREDAREA")
            cell.setPhoneLabelText(text: "_YOURPHONENUMBER")
            return cell
        }
    case .confirmCode:
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhoneNumberCollectionViewCell.name, for: indexPath) as? PhoneNumberCollectionViewCell {
            cell.PhoneNumberTextField.text = self.confirmCode
            cell.setPhoneLabelText(text: "_ENTERCODEFROMSMS")
            cell.setSecurityLabel(text: "_IFYOUDIDNTRECIEVETHESMS")
            cell.PhoneNumberTextField.defaultTextAttributes.updateValue(5.0, forKey: NSAttributedString.Key(rawValue: NSAttributedString.Key.kern.rawValue))
            return cell
        }
    }
    return UICollectionViewCell.init()
}

您有更多选项来执行此操作:

  • 为不同的选项创建两个不同的自定义单元格

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let models = model[indexPath.row]
    
    switch models {
    case .phoneNumber:
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FirstCustomCell.name, for: indexPath) as? FirstCustomCell {
            cell.PhoneNumberTextField.text = self.phoneNumber
            cell.setSecurityLabel(text: "_ALLYOURDATAISINSECUREDAREA")
            cell.setPhoneLabelText(text: "_YOURPHONENUMBER")
            return cell
        }
    case .confirmCode:
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SecondCustomCell.name, for: indexPath) as? SecondCustomCell {
            cell.PhoneNumberTextField.text = self.confirmCode
            cell.setPhoneLabelText(text: "_ENTERCODEFROMSMS")
            cell.setSecurityLabel(text: "_IFYOUDIDNTRECIEVETHESMS")
            cell.PhoneNumberTextField.defaultTextAttributes.updateValue(5.0, forKey: NSAttributedString.Key(rawValue: NSAttributedString.Key.kern.rawValue))
            return cell
        }
    }
    return UICollectionViewCell.init()
    }
    
  • 在PhoneNumberCollectionViewCell上创建两个文本字段并将Alpha设置为0

    class PhoneNumberCollectionViewCell: UICollectionViewCell, NiBLoadable {
    
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var PhoneNumberTextField: TKFormTextField!,
    @IBOutlet weak var CodeFromSmsTextField: UITextField!
    @IBOutlet weak var SecurityLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        Decorator.decorate(self)
        PhoneNumberTextField.alpha = 0
        CodeFromSmsTextField.alpha = 0
    
    }
    
    func setPhoneLabelText(text: String) {
        phoneLabel.text = text
    }
    
    func setSecurityLabel(text: String) {
        SecurityLabel.text = text
    }
    }
    
在这之后,你可以展示你所需要的

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let models = model[indexPath.row]

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhoneNumberCollectionViewCell.name, for: indexPath) as? PhoneNumberCollectionViewCell else { return UICollectionViewCell() }

    switch models {
    case .phoneNumber:
            cell.PhoneNumberTextField.text = self.phoneNumber
            cell.setSecurityLabel(text: "_ALLYOURDATAISINSECUREDAREA")
            cell.setPhoneLabelText(text: "_YOURPHONENUMBER")
            cell.PhoneNumberTextField.alpha = 1
    case .confirmCode:
            cell.PhoneNumberTextField.text = self.confirmCode
            cell.setPhoneLabelText(text: "_ENTERCODEFROMSMS")
            cell.setSecurityLabel(text: "_IFYOUDIDNTRECIEVETHESMS")
            cell.PhoneNumberTextField.defaultTextAttributes.updateValue(5.0, forKey: NSAttributedString.Key(rawValue: NSAttributedString.Key.kern.rawValue))
            cell.CodeFromSmsTextField.alpha = 1
    }
}

添加您的函数
cellForItemAt
function我在下面添加。请看