How pass data to an UICollectionView embedded in a TableViewCell (xCode - iOS - Swift 3)

后端 未结 1 849
广开言路
广开言路 2021-01-07 09:34

I implemented a CollectionView in a TableViewCell but I need read dynamic data for set cells on CollectionView.

I can read data on extension from class TableViewCel

相关标签:
1条回答
  • 2021-01-07 10:20

    The simplest way is create one method inside your MultipleTableViewCell with datasource array that you want to fill with CollectionView. Now call this method in cellForRowAt method of 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()
         }
    }
    

    Now call this method in cellForRowAt and pass the datasource array for 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
    }
    
    0 讨论(0)
提交回复
热议问题