Collectionview in collectionview cell fitting to content












0















I have a collectionview with two sections, each sections have one item. The cell in section contains collectionview and I need to fit height cell to content collectionview.



My first section has a fixed height to 120 because it's horizontal collectionview and height not changed. But for second section number of elements can be changed and I need to display all items (collectionview have scrollEnabled to false and scrolldirecton to vertical).



My problem is I need to display a collectionview with two scrolldirection depends sections and display all items for vertical collectionview.



When I do collectionview.collectionViewLayout.collectionViewContentSize.height I have the good height but I don't know how passed this height to parent viewcontroller. I search a dynamic solution to fixed this.



class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

// MARK: - UICollectionViewDataSource

func numberOfSections(in collectionView: UICollectionView) -> Int {
return presenter.numberOfSections // == 2
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return presenter.numberOfItemsInSection // == 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueCell(InlineDisplayCollectionViewCell.self, for: indexPath)
configureCell(cell, at: indexPath)

return cell
}

// MARK: - UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let type = presenter.typeAtIndexPath(indexPath)
switch type {
case .city:
return CGSize(width: collectionView.frame.width, height: 120)
case .suggestion:
return CGSize(width: collectionView.frame.width, height: 820)
default:
return CGSize.zero
}
}


}


class InlineDisplayCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var collectionView: UICollectionView!

var itemType: ItemModelCollectionType! = .city {
didSet {
updateCell()
}
}
var cities: [City]? {
didSet {
collectionView.reloadData()
}
}
var suggestions: [Suggestion]? {
didSet {
collectionView.reloadData()
}
}

override func awakeFromNib() {
super.awakeFromNib()

configureView()
}

// MARK: - UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return itemType == .city ? cities?.count ?? 0 : suggestions?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell
if itemType == .city {
let cityCell = collectionView.dequeueCell(CityCollectionViewCell.self, for: indexPath)
configure(cell: cityCell, at: indexPath)
cell = cityCell
} else {
let suggestionCell = collectionView.dequeueCell(SquareCollectionViewCell.self, for: indexPath)
configure(cell: suggestionCell, at: indexPath)
cell = suggestionCell
}
return cell
}

// MARK: - UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return itemType == .city ? 10 : 10
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return itemType == .city ? 0 : 10
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size: CGSize
if itemType == .city {
size = CGSize(width: 100, height: 120)
} else {
let width = (collectionView.frame.width - (collectionView.contentInset.left + collectionView.contentInset.right) - 10) * 0.5
let height = width
size = CGSize(width: width, height: height)
}

return size
}

// MARK: - Private functions

private func configureView() {
collectionView.delegate = self
collectionView.dataSource = self
collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
collectionView.registerNib(CityCollectionViewCell.self, bundle: nil)
collectionView.registerNib(SquareCollectionViewCell.self, bundle: nil)
}

private func updateCell() {
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = itemType == .city ? .horizontal : .vertical
}
collectionView.isScrollEnabled = itemType == .city
}

private func configure(cell: CityCollectionViewCell, at indexPath: IndexPath) {
guard let city = cities?[indexPath.row] else { return }
cell.configure(with: city)
}

private func configure(cell: SquareCollectionViewCell, at indexPath: IndexPath) {
guard let suggestion = suggestions?[indexPath.row] else { return }
cell.configure(with: suggestion)
}

}









share|improve this question



























    0















    I have a collectionview with two sections, each sections have one item. The cell in section contains collectionview and I need to fit height cell to content collectionview.



    My first section has a fixed height to 120 because it's horizontal collectionview and height not changed. But for second section number of elements can be changed and I need to display all items (collectionview have scrollEnabled to false and scrolldirecton to vertical).



    My problem is I need to display a collectionview with two scrolldirection depends sections and display all items for vertical collectionview.



    When I do collectionview.collectionViewLayout.collectionViewContentSize.height I have the good height but I don't know how passed this height to parent viewcontroller. I search a dynamic solution to fixed this.



    class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    // MARK: - UICollectionViewDataSource

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    return presenter.numberOfSections // == 2
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return presenter.numberOfItemsInSection // == 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueCell(InlineDisplayCollectionViewCell.self, for: indexPath)
    configureCell(cell, at: indexPath)

    return cell
    }

    // MARK: - UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let type = presenter.typeAtIndexPath(indexPath)
    switch type {
    case .city:
    return CGSize(width: collectionView.frame.width, height: 120)
    case .suggestion:
    return CGSize(width: collectionView.frame.width, height: 820)
    default:
    return CGSize.zero
    }
    }


    }


    class InlineDisplayCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionView: UICollectionView!

    var itemType: ItemModelCollectionType! = .city {
    didSet {
    updateCell()
    }
    }
    var cities: [City]? {
    didSet {
    collectionView.reloadData()
    }
    }
    var suggestions: [Suggestion]? {
    didSet {
    collectionView.reloadData()
    }
    }

    override func awakeFromNib() {
    super.awakeFromNib()

    configureView()
    }

    // MARK: - UICollectionViewDataSource

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return itemType == .city ? cities?.count ?? 0 : suggestions?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: UICollectionViewCell
    if itemType == .city {
    let cityCell = collectionView.dequeueCell(CityCollectionViewCell.self, for: indexPath)
    configure(cell: cityCell, at: indexPath)
    cell = cityCell
    } else {
    let suggestionCell = collectionView.dequeueCell(SquareCollectionViewCell.self, for: indexPath)
    configure(cell: suggestionCell, at: indexPath)
    cell = suggestionCell
    }
    return cell
    }

    // MARK: - UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return itemType == .city ? 10 : 10
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return itemType == .city ? 0 : 10
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let size: CGSize
    if itemType == .city {
    size = CGSize(width: 100, height: 120)
    } else {
    let width = (collectionView.frame.width - (collectionView.contentInset.left + collectionView.contentInset.right) - 10) * 0.5
    let height = width
    size = CGSize(width: width, height: height)
    }

    return size
    }

    // MARK: - Private functions

    private func configureView() {
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    collectionView.registerNib(CityCollectionViewCell.self, bundle: nil)
    collectionView.registerNib(SquareCollectionViewCell.self, bundle: nil)
    }

    private func updateCell() {
    if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.scrollDirection = itemType == .city ? .horizontal : .vertical
    }
    collectionView.isScrollEnabled = itemType == .city
    }

    private func configure(cell: CityCollectionViewCell, at indexPath: IndexPath) {
    guard let city = cities?[indexPath.row] else { return }
    cell.configure(with: city)
    }

    private func configure(cell: SquareCollectionViewCell, at indexPath: IndexPath) {
    guard let suggestion = suggestions?[indexPath.row] else { return }
    cell.configure(with: suggestion)
    }

    }









    share|improve this question

























      0












      0








      0








      I have a collectionview with two sections, each sections have one item. The cell in section contains collectionview and I need to fit height cell to content collectionview.



      My first section has a fixed height to 120 because it's horizontal collectionview and height not changed. But for second section number of elements can be changed and I need to display all items (collectionview have scrollEnabled to false and scrolldirecton to vertical).



      My problem is I need to display a collectionview with two scrolldirection depends sections and display all items for vertical collectionview.



      When I do collectionview.collectionViewLayout.collectionViewContentSize.height I have the good height but I don't know how passed this height to parent viewcontroller. I search a dynamic solution to fixed this.



      class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

      // MARK: - UICollectionViewDataSource

      func numberOfSections(in collectionView: UICollectionView) -> Int {
      return presenter.numberOfSections // == 2
      }

      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return presenter.numberOfItemsInSection // == 1
      }

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueCell(InlineDisplayCollectionViewCell.self, for: indexPath)
      configureCell(cell, at: indexPath)

      return cell
      }

      // MARK: - UICollectionViewDelegateFlowLayout

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      let type = presenter.typeAtIndexPath(indexPath)
      switch type {
      case .city:
      return CGSize(width: collectionView.frame.width, height: 120)
      case .suggestion:
      return CGSize(width: collectionView.frame.width, height: 820)
      default:
      return CGSize.zero
      }
      }


      }


      class InlineDisplayCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

      @IBOutlet weak var collectionView: UICollectionView!

      var itemType: ItemModelCollectionType! = .city {
      didSet {
      updateCell()
      }
      }
      var cities: [City]? {
      didSet {
      collectionView.reloadData()
      }
      }
      var suggestions: [Suggestion]? {
      didSet {
      collectionView.reloadData()
      }
      }

      override func awakeFromNib() {
      super.awakeFromNib()

      configureView()
      }

      // MARK: - UICollectionViewDataSource

      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return itemType == .city ? cities?.count ?? 0 : suggestions?.count ?? 0
      }

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell: UICollectionViewCell
      if itemType == .city {
      let cityCell = collectionView.dequeueCell(CityCollectionViewCell.self, for: indexPath)
      configure(cell: cityCell, at: indexPath)
      cell = cityCell
      } else {
      let suggestionCell = collectionView.dequeueCell(SquareCollectionViewCell.self, for: indexPath)
      configure(cell: suggestionCell, at: indexPath)
      cell = suggestionCell
      }
      return cell
      }

      // MARK: - UICollectionViewDelegateFlowLayout

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
      return itemType == .city ? 10 : 10
      }

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
      return itemType == .city ? 0 : 10
      }

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      let size: CGSize
      if itemType == .city {
      size = CGSize(width: 100, height: 120)
      } else {
      let width = (collectionView.frame.width - (collectionView.contentInset.left + collectionView.contentInset.right) - 10) * 0.5
      let height = width
      size = CGSize(width: width, height: height)
      }

      return size
      }

      // MARK: - Private functions

      private func configureView() {
      collectionView.delegate = self
      collectionView.dataSource = self
      collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
      collectionView.registerNib(CityCollectionViewCell.self, bundle: nil)
      collectionView.registerNib(SquareCollectionViewCell.self, bundle: nil)
      }

      private func updateCell() {
      if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
      flowLayout.scrollDirection = itemType == .city ? .horizontal : .vertical
      }
      collectionView.isScrollEnabled = itemType == .city
      }

      private func configure(cell: CityCollectionViewCell, at indexPath: IndexPath) {
      guard let city = cities?[indexPath.row] else { return }
      cell.configure(with: city)
      }

      private func configure(cell: SquareCollectionViewCell, at indexPath: IndexPath) {
      guard let suggestion = suggestions?[indexPath.row] else { return }
      cell.configure(with: suggestion)
      }

      }









      share|improve this question














      I have a collectionview with two sections, each sections have one item. The cell in section contains collectionview and I need to fit height cell to content collectionview.



      My first section has a fixed height to 120 because it's horizontal collectionview and height not changed. But for second section number of elements can be changed and I need to display all items (collectionview have scrollEnabled to false and scrolldirecton to vertical).



      My problem is I need to display a collectionview with two scrolldirection depends sections and display all items for vertical collectionview.



      When I do collectionview.collectionViewLayout.collectionViewContentSize.height I have the good height but I don't know how passed this height to parent viewcontroller. I search a dynamic solution to fixed this.



      class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

      // MARK: - UICollectionViewDataSource

      func numberOfSections(in collectionView: UICollectionView) -> Int {
      return presenter.numberOfSections // == 2
      }

      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return presenter.numberOfItemsInSection // == 1
      }

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueCell(InlineDisplayCollectionViewCell.self, for: indexPath)
      configureCell(cell, at: indexPath)

      return cell
      }

      // MARK: - UICollectionViewDelegateFlowLayout

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      let type = presenter.typeAtIndexPath(indexPath)
      switch type {
      case .city:
      return CGSize(width: collectionView.frame.width, height: 120)
      case .suggestion:
      return CGSize(width: collectionView.frame.width, height: 820)
      default:
      return CGSize.zero
      }
      }


      }


      class InlineDisplayCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

      @IBOutlet weak var collectionView: UICollectionView!

      var itemType: ItemModelCollectionType! = .city {
      didSet {
      updateCell()
      }
      }
      var cities: [City]? {
      didSet {
      collectionView.reloadData()
      }
      }
      var suggestions: [Suggestion]? {
      didSet {
      collectionView.reloadData()
      }
      }

      override func awakeFromNib() {
      super.awakeFromNib()

      configureView()
      }

      // MARK: - UICollectionViewDataSource

      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return itemType == .city ? cities?.count ?? 0 : suggestions?.count ?? 0
      }

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell: UICollectionViewCell
      if itemType == .city {
      let cityCell = collectionView.dequeueCell(CityCollectionViewCell.self, for: indexPath)
      configure(cell: cityCell, at: indexPath)
      cell = cityCell
      } else {
      let suggestionCell = collectionView.dequeueCell(SquareCollectionViewCell.self, for: indexPath)
      configure(cell: suggestionCell, at: indexPath)
      cell = suggestionCell
      }
      return cell
      }

      // MARK: - UICollectionViewDelegateFlowLayout

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
      return itemType == .city ? 10 : 10
      }

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
      return itemType == .city ? 0 : 10
      }

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      let size: CGSize
      if itemType == .city {
      size = CGSize(width: 100, height: 120)
      } else {
      let width = (collectionView.frame.width - (collectionView.contentInset.left + collectionView.contentInset.right) - 10) * 0.5
      let height = width
      size = CGSize(width: width, height: height)
      }

      return size
      }

      // MARK: - Private functions

      private func configureView() {
      collectionView.delegate = self
      collectionView.dataSource = self
      collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
      collectionView.registerNib(CityCollectionViewCell.self, bundle: nil)
      collectionView.registerNib(SquareCollectionViewCell.self, bundle: nil)
      }

      private func updateCell() {
      if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
      flowLayout.scrollDirection = itemType == .city ? .horizontal : .vertical
      }
      collectionView.isScrollEnabled = itemType == .city
      }

      private func configure(cell: CityCollectionViewCell, at indexPath: IndexPath) {
      guard let city = cities?[indexPath.row] else { return }
      cell.configure(with: city)
      }

      private func configure(cell: SquareCollectionViewCell, at indexPath: IndexPath) {
      guard let suggestion = suggestions?[indexPath.row] else { return }
      cell.configure(with: suggestion)
      }

      }






      swift uicollectionview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 23:39









      fl0_9fl0_9

      135




      135
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272306%2fcollectionview-in-collectionview-cell-fitting-to-content%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272306%2fcollectionview-in-collectionview-cell-fitting-to-content%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Callistus III

          Plistias Cous

          Index Sanctorum