【问题标题】:How pass data to an UICollectionView embedded in a TableViewCell (xCode - iOS - Swift 3)如何将数据传递给嵌入在 TableViewCell 中的 UICollectionView (xCode - iOS - Swift 3)
【发布时间】:2017-07-27 19:58:27
【问题描述】:

我在 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)
        }

    }

【问题讨论】:

  • 在视图控制器的索引路径的行单元格中,你能不能只说 cell.collectionView.dataSource = self?

标签: ios xcode uitableview swift3 uicollectionview


【解决方案1】:

最简单的方法是在您的MultipleTableViewCell 中创建一个方法,其中包含您要使用CollectionView 填充的数据源数组。现在在TableViewcellForRowAt方法中调用这个方法。

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多