Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在不同CollectionViewCell之间传递数据_Ios_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Ios 在不同CollectionViewCell之间传递数据

Ios 在不同CollectionViewCell之间传递数据,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我想将数据从FeedCollectionViewCell传递到另一个ChatViewController 内部馈送视图控制器 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ let cell = collectionView.dequeueReusableCell(withReuseIdentifier:

我想将数据从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

干管的型号如下所示:

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旁边有一个按钮,人们可以单击该按钮与用户聊天。单击该按钮后,它会将用户引导到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中的username进行协调。

首先,向您的
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
    方法中执行任何操作、查看控制器演示文稿等。每次重新绘制集合视图时,系统都会为每个项目调用它,因此这将是一种混乱。它仅用于设置每个单元格的外观

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

  • 我不确定你的
    按钮按钮是做什么的。这是按下按钮时调用的闭合吗?这就是为什么我选择在选择单元格时显示新的视图控制器

  • 您还可以使用自定义序列图像板序列执行此操作,并使用以下方法:
    performsgue
    显示新的视图控制器,以及
    prepareforsgue
    在新视图控制器显示之前将数据发送到新视图控制器。这将代替
    self.present()


这是您在这方面的最小问题code@malhal这只是代码的一部分,如果我在中跳过整个区块会更好吗?不,检查主细节应用程序模板并了解PrepareForegue,也许不将帖子数组反向排序4次?@malhal PrepareForegue用于在两个视图控制器之间传递数据?我正在尝试将数据从一个collectionviewcell传递到另一个collectionviewcell Hi Chris,我修改了我的问题,你能看一下吗?谢谢,洗液会有很好的效果look@LioTan我想不出最好的办法。我看到你在这个问题上贴了一个变体--你得到有用的答案了吗?遗憾的是,我还是想不出答案。