2017-12-28 31 views
1

2つの異なるセクションを持つコレクションビューがあります。セクションの1つでセルをタップして、そのセルのテキストをテキストビューに渡したいとします。テキストビューは、それ自身のセクションの別のセルにあります。複数のUICollectionViewCell間でデータを渡す

これはこれまで私が試したことですが、何も起こりませんでした。ノートデータを別のセルに送信しようとしています。私は、セルがタップされたときにデータを印刷することができます。

:これは、選択したセルデータを渡すテキストビューのセルです。ここで

// 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 
    } 
} 
+0

誰にも指摘されて何を知っていません。 –

+0

@エル・トマト、ご返信ありがとうございます。 notesは文字列の配列で、コレクションビューにデータを格納するために使用しています。 –

+0

@IamWayne 'dequeueReusableCell'の代わりに' cellForItem'を使うべきです。 – trungduc

答えて

1

は、あなたがそれを修正することができる方法である。

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. 
     } 
    } 
} 
+0

あなたのソリューションは私の問題を解決します。どうもありがとうございました! –

関連する問題