Ios CollectionViewControllerCell未显示CollectionViewController的问题

Ios CollectionViewControllerCell未显示CollectionViewController的问题,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在使用Firebase制作一个iPhone应用程序,下面的代码有一个小问题 import UIKit import Firebase class ChatController: UICollectionViewController, UITextFieldDelegate, UICollectionViewDelegateFlowLayout { var game: Game? { didSet { navigationItem.title

我正在使用Firebase制作一个iPhone应用程序,下面的代码有一个小问题

import UIKit
import Firebase

class ChatController: UICollectionViewController, UITextFieldDelegate, UICollectionViewDelegateFlowLayout {
    var game: Game? {
        didSet {
            navigationItem.title = game?.name
        }
    }

    lazy var inputTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "Enter message..."
        textField.translatesAutoresizingMaskIntoConstraints = false
        return textField
    }()

    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.alwaysBounceVertical = true
        collectionView?.backgroundColor = UIColor.white
        collectionView?.register(ChatMessageCell.self,  forCellWithReuseIdentifier: cellId)

        setupInputComponents()
    }

    var messages = [Message]()

    func observeMessages() {
        let ref = Database.database().reference().child("games").child((game?.id)!).child("messages")
        ref.observe(.childAdded, with: { (snapshot) in
            if let dictionary = snapshot.value as? [String: AnyObject] {
                let message = Message()
                message.setValuesForKeys(dictionary)
                self.messages.append(message)

                DispatchQueue.main.async {
                    self.collectionView?.reloadData()
                }
            }
        }, withCancel: nil)
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return messages.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath) as! ChatMessageCell

//        let message = messages[indexPath.item]
//        cell.textView.text = message.text
        cell.backgroundColor = UIColor.blue

        return cell
    }

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

    // Sets up the entire message sending
    // container for the send button and input
    // text field
        func setupInputComponents() {
        // Creates the container for send button
        // and input text field
        let containerView = UIView()
        containerView.backgroundColor = UIColor.white
        containerView.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(containerView)

        // anchor constraints and x,y,w,h
        // for positioning the container
        containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true

        // Creates the send button
        let sendButton = UIButton(type: .system)
        sendButton.setTitle("Send", for: .normal)
        sendButton.translatesAutoresizingMaskIntoConstraints = false
        sendButton.addTarget(self, action: #selector(handleSend), for: .touchUpInside)
        containerView.addSubview(sendButton)

        // Positions the send button within the container
        sendButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        sendButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        sendButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
        sendButton.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

        // Creates the input text field
        containerView.addSubview(inputTextField)

        // Positions the send button within the container
        inputTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 8).isActive = true
        inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
        inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

        // Create a seperator for this component
        let seperatorLineView = UIView()
        seperatorLineView.backgroundColor = UIColor(r: 220, g: 220, b: 220)
        seperatorLineView.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(seperatorLineView)

        // Positions the seperator
        seperatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 8).isActive = true
        seperatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
        seperatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        seperatorLineView.heightAnchor.constraint(equalToConstant: 1).isActive = true
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    @objc func handleSend() {

        let ref = Database.database().reference().child("games").child("-L6lIJt_cw6saNNWMgu1").child("messages")
        ref.keepSynced(true)
        let childRef =  ref.childByAutoId()
        let values = ["text": inputTextField.text!]
        childRef.updateChildValues(values)
    }
}
我有一些注释掉的片段,它们实际上是我想要在单元格中显示为文本的,但我甚至无法在CollectionViewController中为我显示彩色框。DB检索代码工作得非常好,我可以在TableViewController中显示它提供给我的内容。它不在我在这里制作的CollectionViewController中

实际情况是,应用程序成功构建和加载,但当我导航到此屏幕时,我创建的单元格没有显示出来。这些行具体如下:

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return messages.count
//        return 5
}

override func collectionView(_ collectionView: UICollectionView,, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath) as! ChatMessageCell

//        let message = messages[indexPath.item]
//        cell.textView.text = message.text
    cell.backgroundColor = UIColor.blue

    return cell
}


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

您需要添加以下委托和数据源

向类中添加
UICollectionViewDataSource
UICollectionViewDelegate

然后在您的
视图中添加
self.collectionView?.delegate=self
self.collectionView?.dataSource=self

这会告诉视图控制器它的委托和数据源方法在自己的类中。
这应该能解决你的问题。很抱歉,如果此答案中的格式有点不正确。在我的手机上,所以在我键入答案时看不到问题

何时何地调用
observeMessages
如果从未调用过
observeMessages
,则从未填充过
消息
,因此集合视图中不会有任何内容。这对我很有效,谢谢。只是一个小小的改变。当我将这两个扩展添加到我的类中时,它告诉我这是多余的,所以它们必须是我已经扩展的类的一部分。添加这些第二位代码帮助我解决了这个问题!非常感谢。