title: Swift vertical UICollectionView inside UITableView
body: <p>I'm developing an app which requires me to have two types of feed inside my ViewController - Horizontal and Vertical.</p>
  
  <p>The structure is like this: ViewController -> <code>UITableView</code> -> first section has horizontal <code>UICollectionView</code> -> second section has vertical <code>UICollectionView</code>.</p>
  
  <p>My issue is that i'm unable to get the second section of the <code>UITableView</code> (which has inside the Vertical <code>UICollectionView</code>) to have a proper height. It gets interrupted in the middle when I'm scrolling down.</p>
  
  <p>It's important to say that the UICollectionView cell height is not fixed.</p>
  
  <p>I followed some tutorials and guides out there and saw many use cases of <code>UITableViewAutomaticDimension</code>, but couldn't get it to work.</p>
  
  <p>Edit:
  Here is how my storyboard looks like:
  <a href="https://i.stack.imgur.com/SX6wk.png" rel="noreferrer">My StoryBoard</a>
  This is my main ViewController code:</p>
  
  <pre>
  class FeedViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  
      @IBOutlet weak var tableView: UITableView!
  
      var stationArray = Array()
  
      override func viewDidLoad() {
          super.viewDidLoad()d
      }
  
      func numberOfSections(in tableView: UITableView) -> Int {
          return 2
      }
  
      func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
          return categories[section]
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return 1
      }
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          if indexPath.section == 0 {
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FeedHorizontalTableViewCell
              *Some code*
              return cell
          } else {
              let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2") as! FeedVerticalTableViewCell
              *Some code*
              cell2.setNeedsLayout()
              return cell2
          }
      }
  
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
          if section == 1 {
              return 50
          }
  
          return 0
      }
  
      func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
          return 500
      }
  
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
              if indexPath.section == 0 {
                  if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") {
                      return cell.frame.height
                  }
                  return 280
              } else {
                  return UITableViewAutomaticDimension
              }
      }
  }
  </pre>
  
  <p>This is my vertical UITableViewCell code:</p>
  
  <pre>
  class FeedVerticalTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  
      @IBOutlet weak var collectionView: UICollectionView!
  
      var stations = Array() {
          didSet {
              collectionView.reloadData()
          }
      }
  
      override func awakeFromNib() {
          super.awakeFromNib()
      }
  
      override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          return stations.count
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "feedVerticalCell", for: indexPath) as! FeedVerticalCollectionViewCell
          *some code*
          return cell
      }
  }
  </pre>