我想将数据从FeedCollectionViewCell传递到另一个ChatViewController。
内部供稿视图控制器
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! FeedCollectionViewCell
cell.postTextView.text = posts.reversed()[indexPath.row].caption
cell.postImageView.image = UIImage(named: "photoplaceholder.jpg")
cell.priceTextView.text = posts.reversed()[indexPath.row].price
cell.categoryTextView.text = posts.reversed()[indexPath.row].category
cell.usernameLabel.text = posts.reversed()[indexPath.row].username
cell.buttonEvents = {
let storyboard = UIStoryboard(name: "Main" , bundle: nil)
let chatViewController =
storyboard.instantiateViewController(withIdentifier: "chat")
self.present(chatViewController, animated: true,completion: nil)
var receiverIDNumber = cell.usernameLabel.text
}
//我想将receiverIDNumber传递给ChatViewController
ChatViewController内部:
func sendDataToDatabase(message: String){
let ref = Database.database().reference()
let senderIDNumber = Auth.auth().currentUser?.uid
**//I want receiverIDNumber to be the data that is passed from FeedViewController**
let receiverIDNumber = "21"
let postsReference = ref.child("chats")
let newPostId = postsReference.childByAutoId().key
let newPostReference = postsReference.child(newPostId)
newPostReference.setValue(["message" : messageText.text!, "senderID" : senderIDNumber, "receiverID" : receiverIDNumber], withCompletionBlock: {
(error, ref) in
if error != nil {
return
}
})
}
我正在尝试从FeedCollectionViewCell获取receiverIDNumber(将收到该消息的用户)
对于MainViewController中的cell.buttonEvents,它是打开ChatViewController。
Main的模型如下所示:
class Post{
var caption: String
var photoUrl: String
var price: String
var category: String
var username: String
var pic: String
init(captionText: String, photoUrlString: String, priceText: String, categoryText: String, usernameLabel: String, profileImageURL: String){
caption = captionText
photoUrl = photoUrlString
price = priceText
category = categoryText
username = usernameLabel
pic = profileImageURL
}
有关例如在电池,还有就是userprofileimage旁边有一个按钮,人们可以点击与users.Upon聊一下按钮,它会引导他们到ChatViewController.In ChatViewController,我用不同的模式:
class Chat {
var message: String
var senderID: String
var receiverID: String
init(messageTextString: String, senderIDNumber: String, receiverIDNumber: String){
message = messageTextString
senderID = senderIDNumber
receiverID = receiverIDNumber
}
由于receiverIDNumber(receiverUsername)来自ChatCollectionView,我发现难以将其与Main中的用户名进行协调。
答案 0 :(得分:0)
首先,将变量添加到ChatViewController
类:
class ChatViewController: UIViewController {
var username: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Check if userID has been given a value
if !username.isEmpty {
// Do stuff here with username
}
}
}
然后,您可以在实例化视图控制器后为其分配值。我已将其放在didSelectRow
方法中,因此在选择单元格时将出现新的视图控制器。您仍然需要使用cellForItemAt
方法制作每个单元格。
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main" , bundle: nil)
let chatViewController = storyboard.instantiateViewController(withIdentifier: "chat")
// Here is the important bit
chatViewController.username = posts.reversed()[indexPath.row].username
// Present view controller
self.present(chatViewController, animated: true,completion: nil)
}
一些注意事项:
请勿在{{1}}方法中执行任何操作,查看控制器演示文稿等。每次重绘集合视图时,系统都会为每个项目调用它,因此这将是混乱的。设置每个单元格的外观仅。
制作一个新的反向帖子数组,而不是每次需要时将其翻转:cellForItemAt
我不确定你let reversedPosts = posts.reversed()
做了什么。这是按下按钮时调用的闭包吗?这就是我选择单元格时选择呈现新视图控制器的原因。
您也可以使用自定义故事板segue执行此操作,并使用以下方法:buttonEvents
显示新视图控制器,并performSegue
在数据显示之前将数据发送到新视图控制器。这将取代prepareForSegue
等。