TableView Automatic Dimension Tableview Inside Tableview

大憨熊 提交于 2019-11-29 11:08:05

Add these 2 lines before the end of cellForRowAt in main & inner tableViews

  cell.layoutSubviews()

  cell.layoutIfNeeded()

  return cell

Also you may try add layoutSubviews method in main cell subclass and do this

  self.tblOrderMaster.layoutIfNeeded()

and in viewDidLayoutSubviews of controller of the main tableView and do this

  self.tblOrders.layoutIfNeeded()

Try adding this to the tblOrders :

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.contentView.setNeedsLayout()
    cell.contentView.layoutIfNeeded()
}

Why don't you do it based on the rowcount of the inner tableView for example let's say your inner tableview has a rowheight of 30 points than to set the parenttableview rowheight all you have to do is to multiply the rowcount of the child tableview by the rowheight and you will get exactly what you want.

I have tried almost all the possible solutions but each has some problems. So I have found another workaround to fix this It can not be called perfect solution but yes it works for me

In tableview cell, I have added One Hidden Label which is Top : 0 , Bottom : 0 , left : 0 and right : 0

Now

    // This is temp label which help to calulate height to tableview
    if let orderMaster = restObj.orderDataMaster {

        var tempText = ""
        for orderMasterObject in orderMaster {

            tempText += (orderMasterObject.item?.name ?? "") + "\n" + "  "

            if let subItems = orderMasterObject.masterSubitem {

                let result = subItems.reduce("", { (result, subItem) -> String in
                    return result +  (subItem.subitem?.name ?? "") + ","
                })
                tempText += result + "\n" + "  "

            }
            tempText += "\n"
        }
        cell.lblTemp.text = tempText
    }

    self.view.layoutIfNeeded()
    cell.setNeedsLayout()
    cell.delegate = self 

What here done is, Inner tableview's cell has labels which grow as per text. So That temp label has same text (masterSubitem) in my case, in short I have replicated inner tableview cell in that label as we have provided All Four constrains to label so when it will grow our tableview cell will grow along with it.

Hope it is helpful to someone who is facing the same issue If any of you can post another working solution I will happily accept it :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!