0

I'm using UITableViewController and put UICollectionView in second row in Table View Cell. The problem is how I can get heightForRowAt dynamically.

I have tried using return UITableViewAutomaticDimension. But not working.

Any help is really appreciated.

viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.estimatedRowHeight = self.tableView.rowHeight
        self.tableView.rowHeight = UITableViewAutomaticDimension
    }

UITableViewController

Option 1

 override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.tableView.bounds.width + 15
    }

Option 2

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

Currently, I hard code the number of returns.

UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 15
    }

UICollectionViewDelegate, UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumLineSpacing = 2.5
        layout.minimumInteritemSpacing = 2.5

        itemWidth = (collectionView.bounds.width - 5.0) / 3.0
        return CGSize(width: itemWidth, height: itemWidth)
    }

Here is the image of UI using return self.tableView.bounds.width + 15 :

enter image description here

Here is the image of UI using return UITableViewAutomaticDimension :

enter image description here

Here is the image of Constraints :

enter image description here

Nizzam
  • 720
  • 2
  • 11
  • 26
  • what are your constraint for UICollectionView? – Tushar Sharma Apr 19 '17 at 04:07
  • @TusharSharma see updated questions. all set to 0. – Nizzam Apr 19 '17 at 04:11
  • can you show how have you used automatic dimensions? I mean what all steps have you performed? – Tushar Sharma Apr 19 '17 at 04:12
  • I have update the questions. Currently what I am doing is I set the `return self.tableView.bounds.width + 15`. To get a long height. If I use the `UITableViewAutomaticDimension` it will be normal table. – Nizzam Apr 19 '17 at 04:22
  • @TusharSharma, I already update the questions. – Nizzam Apr 19 '17 at 04:25
  • For automatic dimension to work you need to set an estimated height first. Just hardcode the estimated height, and set `UITableViewAutomaticDimension` for height. – GoodSp33d Apr 19 '17 at 04:33
  • How have you set height of collectionview? – RajeshKumar R Apr 19 '17 at 04:39
  • @GoodSp33d Could you please give an example. Hardcode the estimated height ? – Nizzam Apr 19 '17 at 04:39
  • It doesnt work like this. BCZ first your tableview is loaded and it sets its height then collection view is called and then collectionview set its height which is greater then tablecell height. So it takes first loaded tablecell height alays. So you need to forcefully update the tableCell Height after collectionView load. – dahiya_boy Apr 19 '17 at 04:40
  • @RajeshkumarR, No, I didnt set height – Nizzam Apr 19 '17 at 04:40
  • @dahiya_boy, How to forcefully update ? Could you please give suggestion ? – Nizzam Apr 19 '17 at 04:44
  • @Nizzam Tushar has given that example, can you try that and let us know what happens ? – GoodSp33d Apr 19 '17 at 04:50
  • If you have a label in tableviewcell its height will increase based on the label text and its cell height will increase based on label height. Here you have a collectionview in tableviewcell and it doesnt change collectionview height based on its content. Check this http://stackoverflow.com/a/33364092/7250862 – RajeshKumar R Apr 19 '17 at 05:21
  • @RajeshkumarR, I start iOS dev with Swift. No idea on objective-C. :( – Nizzam Apr 19 '17 at 06:48

3 Answers3

0

In your viewDidLoad() try adding this -:

 override func viewDidLoad() {
        super.viewDidLoad()

       yourTableView.estimatedRowHeight = 70 // this is your storyboard default cell height 
       yourtableView.rowHeight = UITableViewAutomaticDimension //Automatic dimensions

    }

Then try this -:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
Tushar Sharma
  • 2,297
  • 1
  • 9
  • 25
  • `self.tableView.estimatedRowHeight = self.tableView.rowHeight` `self.tableView.rowHeight = UITableViewAutomaticDimension` I already implement this. – Nizzam Apr 19 '17 at 04:34
  • Sorry for forget to notify you the result. Its happen same as `return UITableViewAutomaticDimension` in the image. – Nizzam Apr 19 '17 at 04:51
  • What is your suggestion ? – Nizzam Apr 21 '17 at 07:30
0

In your case, you have to give dynamic height for your cell which has a collection view depending on the content height of your collection view, but while loading your table view that time may be your collection view would not be loaded completely thus that time your collection view's content height might not be right.

In order to get the right content height you have to calculate collection view's content height like below:

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

//Your cell which has a collectionview
    if indexPath.row == 1{ 
    let contentHeight = (numberOfCollectionViewcell*itemHeight)+collectionViewContentOffset
    return contentHeight
    }
            return self.tableView.bounds.width + 15
        }
Govind Prajapati
  • 957
  • 7
  • 24
  • I dont think you can achieve this `collectionViewContentOffset` in UITableView protocol. – Nizzam Apr 19 '17 at 04:53
  • collectionViewContentOffset is a fixed value which is set by storyboard or by code so we can get it with the reference of collection view everywhere in the class. – Govind Prajapati Apr 19 '17 at 04:59
0
  1. From CollectionViewDelegate yon can identify when collectionview is fully loaded

  2. get the CollectionView content height.

  3. Send this content height to the tableView VC by creating delegate.

  4. In delegate method reload tableView.

  5. In tableViewCell CollectionView is already reloading so every time when you do above method compiler goes to infinity for reloading tableView and collectionView. For this you need to add a flag (isForcefullyReloadCollectionView), flag is true when tableview is tableview is need to reload otherwise false. You need to add this flag where you going to forcely reload the tableView.

Do this above, If you need further help you can ask. If I got any other solution then I update you.

dahiya_boy
  • 7,885
  • 1
  • 25
  • 39
  • what should I call to reload tableview ? `self.tableView.reloadData` ? – Nizzam Apr 19 '17 at 07:30
  • @Nizzam Yeah BUT IN THE CLASS WHERE YOU HAVE TAKEN THE OUTLET OF TABLEVIEW. – dahiya_boy Apr 19 '17 at 08:55
  • Under protocol of `UICollectionViewDelegate`. At `func ....sizeForItemAt....` I try to `print(collectionView.contentSize.height)`. And it returns 0. – Nizzam Apr 19 '17 at 08:59
  • @Nizzam (uicollectionview-has-been-loaded-completely)[http://stackoverflow.com/questions/14020027/how-do-i-know-that-the-uicollectionview-has-been-loaded-completely] First let the collectionview completely load, then check contentview. – dahiya_boy Apr 19 '17 at 09:12