gpt4 book ai didi

ios - 在不同 CollectionViewCell 之间传递数据

转载 作者:行者123 更新时间:2023-11-30 11:27:56 24 4
gpt4 key购买 nike

我想将数据从 FeedCollectionViewCell 传递到另一个 ChatViewController。

内部 Feed View Controller

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
}

例如,在单元格中,用户个人资料图像旁边有一个按钮,人们可以单击该按钮与用户聊天。单击按钮后,它将引导他们到 ChatViewController。在 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
}

由于接收者IDNumber(receiverUsername)来自ChatCollectionView,我发现很难将其与Main中的用户名协调起来。

最佳答案

首先,向您的 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
}
}
}

然后您可以在实例化 View Controller 后为其分配一个值。我已将其放入 didSelectRow 方法中,因此当选择单元格时将出现新的 View Controller 。您仍然需要使用 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)
}

一些注意事项:

  • 不要在 cellForItemAt 方法中放置任何操作、 View Controller 演示等。每次重绘 Collection View 时,系统都会为每个项目调用它,所以这会很困惑。它用于设置每个单元格的外观。

  • 创建一个新的反向帖子数组,而不是每次需要时都将其反转:let reveredPosts = posts.reversed()

  • 我不确定您的 buttonEvents 做了什么。这是按下按钮时调用的闭包吗?这就是为什么我选择在选择单元格时呈现新的 View Controller 。

  • 您还可以使用自定义 Storyboard Segue 来完成此操作,并使用以下方法:performSegue 呈现新的 View Controller ,使用 prepareForSegue 将数据发送到新 View Controller 出现之前。这将代替 self.present() 等。

关于ios - 在不同 CollectionViewCell 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com