0
我正在嘗試創建消息應用程序。當我嘗試顯示包含UICollectionView
中的消息的單元格時,它們沒有顯示出來。 我首先想給單元格一個紅色的背景並顯示我的模型中的項目。我有消息數組。誰能告訴我爲什麼他們不填充?UICollectionViewCells不填充
import UIKit
import Firebase
class ChatLogController: UICollectionViewController,
UITextFieldDelegate, UICollectionViewDelegateFlowLayout{
var messages = [Message]()
var user: User?{
didSet{
navigationItem.title = user?.name
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(handleBackBtnPressed))
observeMessages()
}
}
lazy var inputTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Enter Message..."
textField.translatesAutoresizingMaskIntoConstraints = false
textField.delegate = self
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.white
collectionView?.register(ChatMessageCell.self, forCellWithReuseIdentifier: "cell")
collectionView?.delegate = self
collectionView?.dataSource = self
setupInputComponents()
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ChatMessageCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//set the size
return CGSize(width: self.view.frame.width, height: 80)
}
func setupInputComponents(){
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
//constraints
containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
containerView.backgroundColor = UIColor.white
let sendBtn = UIButton(type: .system)
sendBtn.setTitle("Send", for: .normal)
sendBtn.translatesAutoresizingMaskIntoConstraints = false
sendBtn.addTarget(self, action: #selector(handleSend), for: .touchUpInside)
containerView.addSubview(sendBtn)
sendBtn.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
sendBtn.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
sendBtn.widthAnchor.constraint(equalToConstant: 80).isActive = true
sendBtn.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
containerView.addSubview(inputTextField)
inputTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 8).isActive = true
inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
inputTextField.rightAnchor.constraint(equalTo: sendBtn.leftAnchor).isActive = true
inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
let separator = UIView()
separator.backgroundColor = UIColor.lightGray
separator.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(separator)
separator.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
separator.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
separator.widthAnchor.constraint(equalTo:containerView.widthAnchor).isActive = true
separator.heightAnchor.constraint(equalToConstant: 1).isActive = true
}
func handleSend(){
let reference = FIRDatabase.database().reference().child("messages")
let childRef = reference.childByAutoId()
let toId = user!.id!
let fromId = FIRAuth.auth()!.currentUser!.uid
let timeStamp = Date().timeIntervalSince1970
let values = ["text":inputTextField.text!,"toId":toId,"fromId":fromId,"timeStamp":timeStamp] as [String : AnyObject]
childRef.updateChildValues(values) { (error, ref) in
if error != nil{
print(error?.localizedDescription ?? "")
return
}
let userMessagesRef = FIRDatabase.database().reference().child("user-messages").child(fromId)
let messageId = childRef.key
userMessagesRef.updateChildValues([messageId:1])
let recipientUserMessageRef = FIRDatabase.database().reference().child("user-messages").child(toId)
recipientUserMessageRef.updateChildValues([messageId:1])
}
}
func handleBackBtnPressed(){
let messageController = MessagesVC()
let navController = UINavigationController(rootViewController: messageController)
self.present(navController, animated: true, completion: nil)
}
func observeMessages(){
guard let uid = FIRAuth.auth()?.currentUser?.uid else{
return
}
let ref = FIRDatabase.database().reference().child("user-messages").child(uid)
ref.observe(.childAdded, with: { (snapshot) in
let messageId = snapshot.key
let messagesRef = FIRDatabase.database().reference().child("messages").child(messageId)
messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String:AnyObject] else{
return
}
let message = Message()
message.setValuesForKeys(dictionary)
self.messages.append(message)
DispatchQueue.main.async {
self.collectionView?.reloadData()
}
})
})
}
}
這裏是ChatMessageCell
import UIKit
class ChatMessageCell: UICollectionViewCell {
let textView: UITextView = {
let tv = UITextView()
tv.text = "my text"
tv.font = UIFont.systemFont(ofSize: 16)
return tv
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.red
}
}
我做了,數組不是空的 – andrewF