Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView inside UITableViewCell with dynamic cell height

I have a dynamic list of items, each item could have different teamplate/layout (and height). And one those item types could have an internal list of items to select from, regularly 5-6 rows, each has different height.

If I try to describe it further, in my scenario I have one tableview (#slave) inside tableviewcells (#master-cell) of another tableview (#master). Moreover cells (#slave-cell) in my #slave tableview could have different height as well. So I need to layout my #slave to have #master automatically calc and update its size.

enter image description here

I have the issue with the inner table (#slave). In case of auto-layout, to fit all the cell space, the table will be collapsed unlike UILabel or other controls. So what I need here is to get the projected size of #slave table and set the height of the #slave = content height of the #slave.

I found similar post and it works if all rows have the same height but I'm using custom rows with dynamic height so the tableView.contentSize.Height gives me invalid result (basically just multiply rowNumbers * estimatedRowHeight, you can see it on the screenshot, master item #3 has 4 inner cells). Even after calling #slave.reloadData I couldn't get that size.

What is the proper way to build that kind of UI?

Source code with a test project attached (Xamarin.iOS)

like image 279
Alexey Strakh Avatar asked Apr 23 '16 03:04

Alexey Strakh


2 Answers

I just ran into the same problem a few days ago,and tried to work it around.

The #master-cell works like a childViewController,it's the delegate datasource of the #slave TableViewController.But you cann't have a childViewController in the UITableViewcell.

Customize UITableViewCell to hold necessary property and acts as #slave TableViewController's delegate datasource,and configure #slave-cell's height and data.

The real problem is the height for #master-cell,

  1. If your data is simple and static,you can compute the height in advance,and return it in method func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat of the ViewController.
  2. Otherwise,add a method to #master-cell which return the height for the whole cell when its property is set.And create a proxy #master-cell to compute the height and return it :

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
      let cell   = CustomUITableViewCell();
      let model  = self.getModel(indexPath)
      cell.model = model
      let height = cell.requiredHeight()
      return height;
    }    
    

    It's complex and expensive,but it works.

like image 76
wj2061 Avatar answered Sep 19 '22 20:09

wj2061


I think you do not have need of take UITableView inside UITableView. You can take more than one section in UITableView. And use different cellReuseIdentifier. This way your goal will be achieved.

like image 23
Payal Maniyar Avatar answered Sep 20 '22 20:09

Payal Maniyar