I have a UITableView which has 2 sections. In section 1 is a static cell which has a horizontal collectionView inside it.
My question is how do I reference the collectionView in the Controller to reload the collectionView...
Here is my code:
TableView Controller
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "collectionCellID", for: indexPath) as! CollectionTableViewCell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCellID", for: indexPath) as! TableCell
return cell
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if let cell = cell as? CollectionTableViewCell {
cell.collectionView.delegate = self
cell.collectionView.dataSource = self
cell.collectionView.contentInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
}
}
}
TableView Cell
class CollectionTableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
}
CollectionView extension
extension MyController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return cell
}
Data call in TableViewController
public func getData() {
ref.observe(.childAdded, with: { (snapshot) in
self.data.append(snapshot)
}
DispatchQueue.main.async {
//MARK: - collectionView.reloadData() <- not available
}
}
})
}