-1
我想在swift中使用JSQMessagesViewController構建一個聊天應用程序。不幸的是,消息不會出現在屏幕上。唯一可見的是輸入欄。消息不顯示在JSQMessagesViewController(swift)
我跟着本教程中的說明:https://www.raywenderlich.com/122148/firebase-tutorial-real-time-chat除了我沒有演示程序STRAT,但用我自己的應用爲出發點。因此,我不使用故事板或導航控制器。
這是我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)
我試圖清理沒有成功的項目。有人可以幫我顯示消息泡沫嗎? 感謝您的幫助!親切的問候, 亞歷
你用'indexPath.row'而不是'indexPath.item'試過了嗎? – Zico
謝謝,我剛剛做到了。但它沒有改變任何東西。 – Alexander
好吧,其實我不知道那個庫,這是我知道的唯一可能導致你的問題的東西。 – Zico