【问题标题】:Passing data between multiple UICollectionViewCell在多个 UICollectionViewCell 之间传递数据
【发布时间】:2017-12-28 03:40:20
【问题描述】:

我有一个包含 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
    }
}

【问题讨论】:

  • 没有人知道音符是什么。
  • @El Tomato,感谢您的回复。 notes 是一个字符串数组,我用它来填充集合视图。
  • @IamWayne 你应该使用cellForItem 而不是dequeueReusableCell
  • @trungduc 感谢您的回复,我是 swift 和收藏视图的新手。但我使用 cellForItem 在集合视图单元格中显示数据。只是不知道如何将数据从一个单元格传递到另一个单元格的单独部分。
  • 你应该使用你想要放入文本的单元格的 indexPath 来 dequeueReusableCell,而不是你点击的单元格。

标签: swift swift3 uicollectionview uicollectionviewcell


【解决方案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.
        }
    }
}

【讨论】:

  • 您的解决方案解决了我的问题。非常感谢!
猜你喜欢
  • 2020-04-05
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多