所以我在过去的6个多小时里一直在弄乱这个视图控制器而没有确定的结果。基本上,在从上一个视图(联系线程列表)中单击UICollectionViewCell后,我将UICollectionView(聊天记录)推入屏幕。每当新视图出现时,零消息出现,print(messages.count)产生0.令人讨厌的是我可以在文本字段中键入消息并点击发送,然后发送消息,然后正确地将消息发送到在线数据库。有人可以告诉我如何在我的在线数据库中获取消息以正确填充视图吗? (或者至少我做错了什么?)
初始代码:
var ref: FIRDatabaseReference!
var messages: [FIRDataSnapshot]! = []
private var _refHandle: FIRDatabaseHandle!
var storageRef: FIRStorageReference! // For images
var clientTable: UICollectionView! // For collectionview editing
var bottomConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
clientTable?.registerClass(ChatLogCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView?.backgroundColor = UIColor.whiteColor()
collectionView?.alwaysBounceVertical = true
tabBarController?.tabBar.hidden = true
// Access root of database
ref = FIRDatabase.database().reference()
// Access root of storage (for non-text media)
storageRef = FIRStorage.storage().referenceForURL("gs://remedy-9403f.appspot.com")
}
override func viewWillAppear(animated: Bool) {
self.messages.removeAll() //possible problem?
// Listen for new messages in the Firebase database
_refHandle = self.ref.child("messages").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
self.messages.append(snapshot)
print(self.messages) //temp
print(self.messages.count) //temp
self.clientTable?.insertItemsAtIndexPaths([NSIndexPath(forRow: self.messages.count - 1, inSection: 1)])
})
self.clientTable?.reloadData()
}
手机代码:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// Dequeue cell
let cell = clientTable.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ChatLogCollectionCell
print(self.messages.count) //temp
// Unpack message from Firebase DataSnapshot
let messageSnapshot: FIRDataSnapshot! = self.messages[indexPath.row]
let message = messageSnapshot.value as! Dictionary<String, String>!
let name = message[Constants.MessageFields.name] as String!
if let imageUrl = message[Constants.MessageFields.imageUrl] {
if imageUrl.hasPrefix("gs://") {
FIRStorage.storage().referenceForURL(imageUrl).dataWithMaxSize(INT64_MAX){ (data, error) in
if let error = error {
print("Error downloading: \(error)")
return
}
cell.imageMessageView.image = UIImage.init(data: data!)
}
} else if let url = NSURL(string:imageUrl), data = NSData(contentsOfURL: url) {
cell.imageMessageView.image = UIImage.init(data: data)
}
cell.messageTextView.text = "sent by: \(name)"
print("Attempted text audit") //temp
} else {
let text = message[Constants.MessageFields.text] as String!
cell.messageTextView.text = name + ": " + text
print("Attempted text audit") //temp
cell.imageMessageView.image = UIImage(named: "")
if let photoUrl = message[Constants.MessageFields.photoUrl], url = NSURL(string:photoUrl), data = NSData(contentsOfURL: url) {
cell.imageMessageView.image = UIImage(data: data)
}
}
// Bubble
if let messageText = message[Constants.MessageFields.text] as String! {
let size = CGSizeMake(250, 1000)
let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
let estimatedFrame = NSString(string: messageText).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(18)], context: nil)
}
return cell
}