Iam Wayne
Iam Wayne

Reputation: 561

Passing data between multiple UICollectionViewCell

I got a collection view with 2 different sections. I want to tap a cell in one of the sections and pass the text in that cell to a text view, which is in a separate cell in its own section.

This is what I tried so far, but nothing happened. I am trying to send the notes data to another cell. I can print the data when the cell is tapped.

Updated: This is the cell with the text view that I want to pass the selected cell data to.

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

Upvotes: 1

Views: 796

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

Here is the way you can correct that:

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

Upvotes: 1

Related Questions