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

在我用代码获得secondCell之后,

我使用相同的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()
}发布于 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: 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 }
在这之后,你可以展示你需要的东西。
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
    }
}https://stackoverflow.com/questions/59926409
复制相似问题