我想将数据从FeedCollectionViewCell传递到另一个ChatViewController。
内部Feed视图控制器
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
}
由于receiverIDNumber(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
}
}
}
然后,您可以在实例化视图控制器之后为其分配一个值。我已将其放在
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)
}
一些注意事项:
请勿在
cellForItemAt
方法中执行任何操作或查看控制器演示等。每次重绘集合视图时,系统都会为每个项目调用它,因此会造成混乱。仅用于设置每个单元格的外观。制作新的反向排列的帖子数组,而不是在每次需要时都将其反转:
let reversedPosts = posts.reversed()
我不确定您
buttonEvents
做什么。这是按下按钮时调用的闭包吗?因此,当我选择一个单元格时,我选择展示新的视图控制器。您也可以使用自定义情节提要剧集来执行此操作,并使用以下方法:
performSegue
呈现新的视图控制器,而prepareForSegue
则在呈现数据之前将数据发送到新的视图控制器。这将代替self.present()
等。关于ios - 在不同的CollectionViewCell之间传递数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50549923/