0

I have a table view with cells growing dynamically using auto layout constraints. I have a scenario in which I have to show UITableView inside one of these dynamically constructed cells. The main thing here is I don't know the frame of anything and I have to construct everything using constraints. All I need is explanation on how to achieve this?

Jaffer Sheriff
  • 1,444
  • 13
  • 33

1 Answers1

0

So I guess you have a MasterViewController which has a tableView in it. Then you want to add another tableView which is part of the class MyCustomcell, inside one of the cell of MasterViewController's tableView.

Adding customCell with tableView is not a big deal, but calculating and maintaining the height of MasterViewController in accordance with the data to be shown in CustomCell is a bit tricky. If you are asking just about how to add CustomCell in a tableView then there are many good tutorials available which you can easily get just by a quick google.

I faced a similar situation and solved the problem in following way. Hope this helps.

1) If you have a customCell where you just have to show texts. Then the height of tableView of MasterViewController can easily be maintained automatically with auto layout depending on the content of texts. This tutorial has a very good explanation.

2) If you are adding tableView inside one of the cell of MasterViewcontroller tableView then it becomes tricky. Now the height of the cell of MasterViewController depends on the content of height of the CustomCell tableview height. I tried to handle this situation by applying different ways in Swift 3 but finally got the result by following this way:-

class MasterViewController: UIViewController {
          @IBOutlet weak var tableView: UITableView!

          var customCell:MyCustomCell? = nil

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell", for: indexPath) as! MyCustomCell
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        customCell = cell
        return cell
    }

}

extension MasterViewController:UITableViewDelegate{

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

         if let aHeight = customCell?.tableView.contentSize.height{
             return aHeight
         }

         return 0

     }

}
skJosh
  • 631
  • 1
  • 8
  • 16
  • What are the constraints that I have to apply to UITableView (which is inside a UITableViewCell) ? – Jaffer Sheriff Dec 02 '16 at 07:39
  • you can just add lead, trail, top, bottom to 0. – skJosh Dec 02 '16 at 08:27
  • I did it but height of tableview is now zero. – Jaffer Sheriff Dec 02 '16 at 08:37
  • Try reloading the table of MaterViewController, or you can just reload the cell in which you have added tableView after the data in MyCustomCell has been populated. In my case I have requested data in MyCustomCell and after the data request is completed and cell has been populated with data, the tableview in MasterViewController has been reloaded. – skJosh Dec 02 '16 at 08:43
  • Could you please share a example project on how to achieve this preferably in github – Jaffer Sheriff Dec 02 '16 at 14:47