如何将数据传递给嵌入在 TableViewCell 中的 UICollectionView (xCode - iOS - Swift 3)

杂草

我在 TableViewCell 中实现了一个 CollectionView,但我需要读取 CollectionView 上设置单元格的动态数据。

我可以从 TableViewCell 类读取扩展数据,也许可以帮助我将数据从 ViewController 传递到 TableViewCell 类。

 class MultipleTableViewCell: UITableViewCell {

        @IBOutlet fileprivate weak var collectionView: UICollectionView!

    }

    extension MultipleTableViewCell : UICollectionViewDataSource {

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 3 // array.count
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "foodCell", for: indexPath) as! UserCollectionViewCell

     let dict = array.object(at: indexPath.row) as! NSDictionary //Like this

            cell.name.text = "How do you read data from ViewController"
            cell.email.text = "How do you read data from ViewController"
            cell.phone.text =  dict["phone"] as? String //Like this
            cell.image.image = "How do you read data from ViewController"

            return cell
        }

    }

    extension MultipleFoodTableViewCell : UICollectionViewDelegateFlowLayout {

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

            let itemsPerRow:CGFloat = 1
            let hardCodedPadding:CGFloat = 20
            let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
            let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
            return CGSize(width: itemWidth, height: itemHeight)
        }

    }
尼拉夫D

最简单的方法是在您MultipleTableViewCell想要填充的数据源数组中创建一个方法CollectionView现在在 的cellForRowAt方法中调用此方法TableView

class MultipleTableViewCell: UITableViewCell {

     @IBOutlet fileprivate weak var collectionView: UICollectionView!

     var array = [String]() //Change with Your array type

     func fillCollectionView(with array: [String]) {
          self.array = array
          self.collectionView.reloadData()
     }
}

现在调用此方法cellForRowAt并将数据源数组传递给collectionView.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier") as! MultipleTableViewCell
    cell.fillCollectionView(with: ["A","B","C"]) //Pass your array
    return cell
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何检测何时在Swift iOS XCode中更改UIView大小?

Swift:UICollectionView iOS行中不同数量的单元格

如何将“领域”数据库预加载到iOS swift3中?

如何使用Swift在Xcode中组织文件以进行iOS开发?

如何将单元格传递给tableView Swift 3中的函数

为什么insetForSectionAt UICollectionView委托未在iOS Swift 3中调用?

Swift 3如何在索引UICollectionView中为每个项目显示不同的数据

Swift / XCode-为什么在按下tableViewCell时不能将数据传递给下一个视图控制器?

MVVMCross iOS如何在TableViewCell按钮中传递参数

如何在静态TableViewCell中显示UICollectionView?

如何将AVPlayerViewController嵌入TableViewCell中

Swift UITableView / UICollectionVIew将数据传递到cellForRowAt中的单元格返回nil

如何将Python3请求中的数据传递给VMware工作站API?

在iOS中摇动tableviewcell

iOS Swift:致命错误,在UICollectionView中展开可选

iOS Swift TableviewCell问题

在Swift 3中更改了TableViewCell复制属性

Swift2 iOS- UICollectionView中的SDWebImage闪烁

将 tableViewCell 中的数据传递给另一个 VC 失败

在 Swift 3 中修复可滚动 TableViewCell 上方的 MapView

如何使用 Swift 3 for iOS 在 ViewController.swift 中实现 UICollectionView

增加/减少值并在 TableViewCell Swift Xcode 内的标签中显示结果

滚动时 TableViewCell 中的 Swift 3 UISwitch 丢失状态

如何将文本字段的值传递给 Swift3 中的按钮单击函数?

如何在swift 3中将Tableviewcell显示为气泡?

Swift 3 - iOS 11.2 上的 UICollectionview 问题

你是如何做到的,所以你不能在 Swift 3 iOS 11 及更高版本的 UICollectionView 中拖动特定单元格?

TableViewCell 无法在 Swift 中获取数据

在 iOS/MacOS 中调用本机函数时,如何将复杂的 swift 数据类型传递给 C 数据类型?