在多个 UICollectionViewCell 之间传递数据

Passing data between multiple UICollectionViewCell

我得到了一个包含 2 个不同部分的集合视图。我想点击其中一个部分中的一个单元格并将该单元格中的文本传递给文本视图,该文本视图位于其自己部分的单独单元格中。

到目前为止,这是我尝试过的方法,但没有任何反应。我正在尝试将笔记数据发送到另一个单元格。我可以在点击单元格时打印数据。

已更新: 这是我要将所选单元格数据传递到的带有文本视图的单元格。

// cell that I am trying to passing data to
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "notesView", for: indexPath) as! TestViewCollectionViewCell
    cell.myTextView.text = .....

    return cell
}


// Cell that I am passing data from
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        // Cell that I want to send data to 
        let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "notesView", for: indexPath) as! TestViewCollectionViewCell

        let myNotes = notes[indexPath.row]
        cell.myTextView.text = myNotes.textView
    }
}

您可以通过以下方式更正:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if indexPath.section == 0 {

        //First get your selected cell
        if let cell = collectionView.cellForItem(at: indexPath) as? TestViewCollectionViewCell {

            //Now get selected cell text here
            //update your section two array with new values
            //Reload your collection view.


        } else {
            // Error indexPath is not on screen: this should never happen.
        }
    }
}