首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >切换textField类

切换textField类
EN

Stack Overflow用户
提问于 2020-01-27 07:02:04
回答 1查看 92关注 0票数 0

,我把CollectionView和CollectionViewCells换了。首先我有这个

在我用代码获得secondCell之后,

我使用相同的sell和不同的方法来更改文本,但是在第一个cell.textField中,作为定制的cocoapod库TKFormTextField,在第二个例子中,我想使用简单的UITextField。什么枚举以及如何使用它?

,这是我的手机

代码语言:javascript
运行
复制
    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()
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-27 09:29:11

您有更多的选择可以这样做:

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

collectionView.dequeueReusableCell(withReuseIdentifier: collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let models = modelindexPath.row switch models { case .phoneNumber: if let cell =collectionView FirstCustomCell.name,for: indexPath) as?FirstCustomCell { cell.PhoneNumberTextField.text = self.phoneNumber cell.setSecurityLabel(文本:"_ALLYOURDATAISINSECUREDAREA") cell.setPhoneLabelText(文本:"_YOURPHONENUMBER")返回单元格} case .confirmCode: if let cell =self.phoneNumber SecondCustomCell.name,for: indexPath) as?cell.PhoneNumberTextField.defaultTextAttributes.updateValue(5.0,SecondCustomCell { cell.PhoneNumberTextField.text = self.confirmCode cell.setPhoneLabelText(text:"_ENTERCODEFROMSMS") cell.setSecurityLabel(text:"_IFYOUDIDNTRECIEVETHESMS") cell.setSecurityLabel forKey: NSAttributedString.Key(rawValue: NSAttributedString.Key.kern.rawValue)返回单元格}ReportUICollectionViewCell.init()}

  • ,在PhoneNumberCollectionViewCell上创建两个文本框,并将alpha设置为0。

类PhoneNumberCollectionViewCell: UICollectionViewCell,NiBLoadable {IBOutlet弱var phoneLabel: UILabel!@IBOutlet弱var PhoneNumberTextField: TKFormTextField!,@IBOutlet弱var CodeFromSmsTextField: UITextField!@IBOutlet弱var SecurityLabel: UILabel!覆盖func awakeFromNib() { super.awakeFromNib() Decorator.decorate(self) PhoneNumberTextField.alpha =0 CodeFromSmsTextField.alpha =0} func setPhoneLabelText(文本: String) { phoneLabel.text = text } func setSecurityLabel(文本: String) { SecurityLabel.text = text }

在这之后,你可以展示你需要的东西。

代码语言:javascript
运行
复制
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
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59926409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档