Я пробовал self.collectionView.reloadData () во многих местах, но я не могу заставить мою коллекцию корректно перезагрузиться, используя данные из массивов "NewArray" и "NewArrayTwo". Что я делаю неправильно?
import UIKit
import FirebaseDatabase
import FirebaseAuth
import Firebase
import FirebaseStorage
class DealClickedViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITextFieldDelegate {
@IBOutlet weak var dealNumberLabel: UILabel!
@IBOutlet weak var eCodeLabel: UILabel!
@IBOutlet weak var userMessageEntryTextField: UITextField!
@IBOutlet weak var collectionView: UICollectionView!
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
var newArray: [String]=[]
var newArrayTwo: [String]=[]
var appendingDoneValue = 0
var ass = 1
override func viewDidLoad() {
let userIDString = self.userID as! String
var messageArrayOne: [String]=[]
var messageSenderArray: [String]=[]
collectionView.dataSource = self
collectionView.delegate = self
//get then set deal number label
ref.child("users").child(userIDString).child("tempData").child("currentDealView").observeSingleEvent(of: .value, with: { (snapshot) in
let dealValue = snapshot.value as! String
self.dealNumberLabel.text = dealValue
//get then set Ecode number
self.ref.child("deals").child("log").child(dealValue).child("eCodeNumber").observeSingleEvent(of: .value, with: { (snapshot) in
let eCodeValue = snapshot.value as! String
self.eCodeLabel.text = eCodeValue
//get the converdation ID
self.ref.child("deals").child("log").child(dealValue).child("conversationID").observeSingleEvent(of: .value, with: { (snapshot) in
let conversationID = snapshot.value as! Int
let conversationString = "conversation"+String(conversationID)
//get number of messages
self.ref.child("conversations").child(conversationString).child("messages").observeSingleEvent(of: .value, with: { (snapshot) in
let tempVarOne = snapshot.childrenCount
let numberOfMessages = Int(tempVarOne)
//append message text of each message to a array, and sender name to a array
var messageProcessCount = numberOfMessages
var messageProcessCountTwo = numberOfMessages
if messageProcessCount >= 1 {
while messageProcessCount > 0 {
//get message text of current message starting with oldest messages
self.ref.child("conversations").child(conversationString).child("messages").child("messageNumber"+String(messageProcessCount)).child("messageText").observeSingleEvent(of: .value, with: { (snapshot) in
let messageText = snapshot.value as! String
//appends message txt to messages array
messageArrayOne.append(messageText)
print("appended Array below")
print(messageArrayOne)
self.newArray = messageArrayOne
})
//increase process count for loop
messageProcessCount = messageProcessCount - 1
}
repeat {
self.ref.child("conversations").child(conversationString).child("messages").child("messageNumber"+String(messageProcessCountTwo)).child("messageSender").observeSingleEvent(of: .value, with: { (snapshot) in
let messageSender = snapshot.value as! String
//appends message txt to messages array
messageSenderArray.append(messageSender)
print("appended Array below")
print(messageSenderArray)
self.newArrayTwo = messageSenderArray
})
//increase process count for loop
messageProcessCountTwo = messageProcessCountTwo - 1
} while messageProcessCountTwo > 0
}
}) }) }) })
super.viewDidLoad()
// Do any additional setup after loading the view.
userMessageEntryTextField.delegate = self
//code to make new chats load at bottom instead of top
collectionView.transform = CGAffineTransform.init(rotationAngle: (-(CGFloat)(Double.pi)))
}
//hide keyboard code starts
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
userMessageEntryTextField.resignFirstResponder()
return true
}
//hide keyboard code ends
//BACK TO DEAL CENTER BUTTON CODE STARTS
@IBAction func backToDealCenter(_ sender: Any) {
backToDC()
}
func backToDC(){
let dealCenterViewControllerTwo = storyboard?.instantiateViewController(identifier: Constants.Storyboard.dealCenterViewControllerTwo) as? DealCenterViewControllerTwo
view.window?.rootViewController = dealCenterViewControllerTwo
view.window?.makeKeyAndVisible()
}
//BACK TO DEAL CENTER BUTTON CODE ENDS
// MESSAGE SEND BUTTON CODE STARTS
@IBAction func userMessageSendButton(_ sender: Any) {
//get the message text to send
let messageToSend = self.userMessageEntryTextField.text
//get the conversation number
let dealNumberAgain = dealNumberLabel.text as! String
ref.child("deals").child("log").child(dealNumberAgain).child("conversationID").observeSingleEvent(of: .value, with: { (snapshot) in
let conversationNumberOnly = snapshot.value as! Int
let conversationNumberOnlyString = String(conversationNumberOnly)
let conversationNumberFullString = "conversation"+conversationNumberOnlyString
//get messageCount
self.ref.child("conversations").child(conversationNumberFullString).child("numberOfMessages").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as! Int
let newNumberOfMessages = value + 1
//update message count
let dictFourtyOne: Dictionary<String, Any> = [
"numberOfMessages": newNumberOfMessages
]
self.ref.child("conversations").child(conversationNumberFullString).updateChildValues(dictFourtyOne, withCompletionBlock: {
(error, ref) in
})
//add message+info to database
let dictFourty: Dictionary<String, Any> = [
"timeSent": "fill later",
"dateSent": "fill later",
"messageText": messageToSend,
"messageSender": self.userID
]
self.ref.child("conversations").child(conversationNumberFullString).child("messages").child("messageNumber"+String(newNumberOfMessages)).updateChildValues(dictFourty, withCompletionBlock: {
(error, ref) in
})
})
})
//refresh the chat
refreshPage()
}
// MESSAGE SEND BUTTON CODE ENDS
//refresh chat function
func refreshPage (){
let dealClickedViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.dealClickedViewController) as? DealClickedViewController
view.window?.rootViewController = dealClickedViewController
view.window?.makeKeyAndVisible()
}
//COLLECTION VIEW CODE STARTS
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.newArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
cell.myLabelTwo.text = self.newArrayTwo[indexPath.item]
cell.myLabel.text = self.newArray[indexPath.item]
cell.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item)
}
/*
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 300, height: 50)
}
*/
//COLLECTION VIEW CODE ENDS
}
Я пробовал self.collectionView.reloadData () во многих местах, но мне не удается правильно перезагрузить представление моей коллекции, используя данные из массивов "NewArray" и "NewArrayTwo". Что я делаю неправильно?