gpt4 book ai didi

swift - 切换文本字段类

转载 作者:行者123 更新时间:2023-11-30 10:27:12 25 4
gpt4 key购买 nike

我有 CollectionView 和我切换的 CollectionViewCells。首先我有这个

I need to set PhoneNumber

在我得到带有代码的第二个单元格后

here is my secondCell

我使用相同的销售方法和不同的方法来更改文本,但在第一个 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
}
}

关于swift - 切换文本字段类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59926409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com