UICollectionViewCell的UiTextField,在UICollectionViewController中重叠

时间:2017-12-24 14:48:47

标签: ios swift uiviewcontroller uicollectionviewcell

我正在尝试设计一个多步骤注册菜单。为此,我使用屏幕大小单元格的UICollectionViewController。在这些单元格中,我有一个UITextView来提问,一个UITextField来收集答案。我还有一个Page对象,用于在设置时从uicollectionviewcontroller传递信息。 我现在遇到的问题是,在每3页之后,我从3页前输入的textField重复,而不是显示占位符。我注意到了另一个问题,单元格似乎只实例化了3次,而不是我有多少页面的6次实例化。实例化顺序也很奇怪。首先它会执行一次,然后按下按钮,再按两次,然后再也不会。 如何解决这个问题,我真的很挣扎,我不知道出了什么问题。

这是我的代码:

import UIKit

class OnboardingPageViewCell: UICollectionViewCell{

    override init(frame: CGRect) {
        super.init(frame: frame)
        print("made a page")
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var oboardingPage = NewOnboardingPage() {
        didSet{
            reload()
        }
    }

    private var questionTextField: UITextView = {
        var q = UITextView()
        q.textColor = UIColor.white
        q.textAlignment = .left
        q.font = UIFont(name: "Avenir-Black", size: 25)
        q.isEditable = true
        q.isScrollEnabled = false
        q.backgroundColor = UIColor.black
        q.translatesAutoresizingMaskIntoConstraints = false
        print("made aquestion field")
        return q
    }()

    private var answerField : CustomTextField = {
        let tf = CustomTextField.nameField
        print("made an answer field")
        return tf
    }()

    private func setupView(){
        backgroundColor = UIColor.white

        addSubview(questionTextField)
        questionTextField.topAnchor.constraint(equalTo: topAnchor, constant: 120).isActive = true
        questionTextField.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        questionTextField.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.90).isActive = true
        questionTextField.heightAnchor.constraint(equalToConstant: 90).isActive = true

        addSubview(answerField)
        answerField.topAnchor.constraint(equalTo: questionTextField.bottomAnchor, constant: 20).isActive = true
        answerField.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        answerField.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.90).isActive = true
        answerField.heightAnchor.constraint(equalToConstant: 90).isActive = true
    }

    private func reload(){
        questionTextField.text = oboardingPage.question
        answerField.placeholder = oboardingPage.answerField
    }
}

class NewOnboardingPage {
    var question : String?
    var answerField : String?
}


import UIKit

class SignUpController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    private let cellId = "cellId"
    private var pages = [NewOnboardingPage]()

    override func viewDidLoad() {
        super .viewDidLoad()
        setupSignUpControllerView()
        addPages()
    }

    private  func addPages(){
        let namePage = NewOnboardingPage()
        namePage.question = "What's your name?"
        namePage.answerField = "What's your name?"
        pages.append(namePage)

        let birthDayPage = NewOnboardingPage()
        birthDayPage.question = "When's your birthdate?"
        birthDayPage.answerField = "When's your birthdate?"
        pages.append(birthDayPage)

        let userNamePage = NewOnboardingPage()
        userNamePage.question = "Choose a user name."
        userNamePage.answerField = "Choose a user name."
        pages.append(userNamePage)

        let passWordPage = NewOnboardingPage()
        passWordPage.question = "Set a password"
        passWordPage.answerField = "Set a password"
        pages.append(passWordPage)

        let emailAuthPage = NewOnboardingPage()
        emailAuthPage.question = "What's your email?"
        emailAuthPage.answerField = "What's your email?"
        pages.append(emailAuthPage)

        let phoneNumberPage = NewOnboardingPage()
        phoneNumberPage.question = "What's your phone number?"
        phoneNumberPage.answerField = "What's your phone number?"
        pages.append(phoneNumberPage)
    }

    private func setupSignUpControllerView(){
        collectionView?.backgroundColor = .white
        collectionView?.register(OnboardingPageViewCell.self, forCellWithReuseIdentifier: cellId)
        collectionView?.isPagingEnabled = true
        collectionView?.isScrollEnabled = true

        if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
            layout.minimumLineSpacing = 0
        }

        view.addSubview(nextButton)
        nextButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 400).isActive = true
        nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        nextButton.widthAnchor.constraint(equalToConstant: 250).isActive = true
        nextButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
    }

    private let nextButton: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = UIColor.RED
        button.setTitle("next", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = UIFont(name: "Avenir-Black", size: 25)
        button.layer.cornerRadius = 30
        button.translatesAutoresizingMaskIntoConstraints = false
        button.addTarget(self, action: #selector(turnNextPage), for: .touchUpInside)
        return button
    }()

    @objc private func turnNextPage() {
        let visibleItems: NSArray = collectionView?.indexPathsForVisibleItems as! NSArray
        let currentItem: IndexPath = visibleItems.object(at: 0) as! IndexPath
        let nextItem: IndexPath = IndexPath(item: currentItem.item + 1, section: 0)
        if nextItem.row < pages.count {
            collectionView?.scrollToItem(at: nextItem, at: .left, animated: true)
        }
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pages.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! OnboardingPageViewCell
        cell.oboardingPage = pages[indexPath.item]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }

}


import UIKit

class CustomTextField: UITextField, UITextFieldDelegate {
convenience init() {
        self.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

        self.allowsEditingTextAttributes = false
        self.autocorrectionType = .no

        self.tintColor = UIColor.RED
        self.translatesAutoresizingMaskIntoConstraints = false
    }


    override func willMove(toSuperview newSuperview: UIView?) {
        addTarget(self, action: #selector(editingChanged), for: .editingChanged)
        editingChanged(self)
    }

    @objc func editingChanged(_ textField: UITextField) {
        guard let text = textField.text else { return }
        textField.text = String(text.prefix(30))
    }

    override func selectionRects(for range: UITextRange) -> [Any] {
        return []
    }

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(paste(_:)) ||
            action == #selector(cut(_:)) ||
            action == #selector(copy(_:)) ||
            action == #selector(select(_:)) ||
            action == #selector(selectAll(_:)){
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }

}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! OnboardingPageViewCell
        cell.answerField.text = nil
        cell.oboardingPage = pages[indexPath.item]
        return cell
    }

1 个答案:

答案 0 :(得分:1)

1- textFeild 显示相同的数据而不是占位符因为单元格出列而导致因此您必须挂钩这些属性并在 cellForRowAt <中清除其内容/ p>

2-实例化是3而不是6个aslo cell dequeuing

解:

为您的模型添加两个属性 NewOnboardingPage 将它们命名为currentQuestion和currentAnswer,并将其作为用户输入并滚动到下一页,将它们保存在模块中,您应该将其设置为全局访问单元格和外部集合滚动到 cellForRowAt

时,这些值会显示在 textfeild textView
相关问题