消息未显示在 JSQMessagesViewController 中 (swift)

Messages do not show up in JSQMessagesViewController (swift)

我正在尝试使用 JSQMessagesViewController 在 swift 中构建一个聊天应用程序。不幸的是,消息不会出现在屏幕上。唯一可见的是输入栏。

我遵循了本教程中的说明:https://www.raywenderlich.com/122148/firebase-tutorial-real-time-chat 除了我没有使用演示应用程序,而是使用我自己的应用程序作为起点。因此,我没有使用故事板或导航控制器。

这是我的聊天控制器:

import Foundation
import UIKit
import Foundation
import JSQMessagesViewController

class chatController: JSQMessagesViewController {

var messages = [JSQMessage]()
var outgoingBubbleImageView: JSQMessagesBubbleImage!
var incomingBubbleImageView: JSQMessagesBubbleImage!

private func setupBubbles() {
    let factory = JSQMessagesBubbleImageFactory()
    outgoingBubbleImageView = factory.outgoingMessagesBubbleImageWithColor(UIColor.jsq_messageBubbleBlueColor())
    incomingBubbleImageView = factory.incomingMessagesBubbleImageWithColor(UIColor.jsq_messageBubbleLightGrayColor())
}

override func viewDidLoad() {
    super.viewDidLoad()
    title = "Sprout"
    setupBubbles()
    collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSizeZero
    collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero

    print(self.view.frame)
}

override func viewDidLayoutSubviews() {
    self.collectionView.frame = self.view.frame
    //self.collectionView.setNeedsLayout()
    //self.collectionView.layoutIfNeeded()
    print("collectionview frame: \(self.collectionView.frame)")
    print("view frame: \(self.view.frame)")
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // messages from someone else
    addMessage("foo", text: "Hey person!")
    // messages sent from local sender
    addMessage(senderId, text: "Yo!")
    addMessage(senderId, text: "I like turtles!")
    // animates the receiving of a new message on the view
    finishReceivingMessage()
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
    let message = messages[indexPath.item] // 1
    if message.senderId == senderId { // 2
        return outgoingBubbleImageView
    } else { // 3
        return incomingBubbleImageView
    }
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
    let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell

    let message = messages[indexPath.item]

    if message.senderId == senderId {
        cell.textView!.textColor = UIColor.whiteColor()
    } else {
        cell.textView!.textColor = UIColor.blackColor()
    }

    return cell
}

override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {

    self.addMessage(senderId, text: text);
    finishSendingMessage()
}

func addMessage(id: String, text: String) {
    print("add message: \(text)")
    let message = JSQMessage(senderId: id, displayName: "", text: text)
    messages.append(message)
}
}

我正在展示从我的 rootViewController 调用此代码:

let userID = "abc123"
let userName = "randomUserName"
let chatVc = chatController()
chatVc.senderId = userID
chatVc.senderDisplayName = userName
self.presentViewController(chatVc, animated: true, completion: nil)

我尝试清理项目但没有成功。有人可以帮我显示消息气泡吗? 谢谢你的帮助!亲切的问候, 亚历克斯

所以我现在修复了它,我缺少两个功能:

override func collectionView(collectionView: JSQMessagesCollectionView!, 
messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
  return messages[indexPath.item]
}

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

我一定是在学习教程的时候监督了他们。在@Zico 的帮助下,我找到了错误并修复了它。