消息不会出现在JSQMessagesViewController(swift)中

时间:2016-09-16 11:51:29

标签: swift view jsqmessagesviewcontroller

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

This is what my view looks like

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

这是我的chatController:

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)

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

1 个答案:

答案 0 :(得分:1)

所以我现在修好了,我错过了两个功能:

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的帮助下,我发现了错误并修复了它。