我正在使用https://www.raywenderlich.com/122148/firebase-tutorial-real-time-chat,我已经达到了#34;创建消息"部分和我的集合视图似乎从未被调用过。我必须在集合视图函数之前取出所有的override语句,因为我遇到了错误。我尝试用
重新加载集合视图 self.collectionView!.reload()
有什么想法吗? 这是我的代码:
Created by Ari on 7/19/16.
// Copyright © 2016 Logical Nonsense LLC. All rights reserved.
//
import UIKit
import JSQMessagesViewController
class ChatViewController: JSQMessagesViewController {
var messages = [JSQMessage]()
var outgoingBubbleImageView: JSQMessagesBubbleImage!
var incomingBubbleImageView: JSQMessagesBubbleImage!
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.reloadData()
print("hi")
title = "ChatChat"
setupBubbles()
// Do any additional setup after loading the view.
collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize(width: 0, height: 0)
collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize(width: 0, height: 0)
addMessage(id: "foo", text: "Hey person!")
// messages sent from local sender
addMessage(id: senderId, text: "Yo!")
addMessage(id: senderId, text: "I like turtles!")
print("hi")
// animates the receiving of a new message on the view
finishReceivingMessage()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.collectionView!.reloadData()
// messages from someone else
addMessage(id: "foo", text: "Hey person!")
// messages sent from local sender
addMessage(id: senderId, text: "Yo!")
addMessage(id: senderId, text: "I like turtles!")
print("hi")
// animates the receiving of a new message on the view
finishReceivingMessage()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: JSQMessagesCollectionView!,
messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return messages.count
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidAppear(true)
}
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
}
}
func collectionView(collectionView: JSQMessagesCollectionView!,
avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}
func addMessage(id: String, text: String) {
print(text)
let message = JSQMessage(senderId: id, displayName: "", text: text)
messages.append(message!)
self.collectionView!.reloadData()
}
private func setupBubbles() {
let factory = JSQMessagesBubbleImageFactory()
outgoingBubbleImageView = factory?.outgoingMessagesBubbleImage(
with: UIColor.jsq_messageBubbleBlue())
incomingBubbleImageView = factory?.incomingMessagesBubbleImage(
with: UIColor.jsq_messageBubbleLightGray())
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}