Dynamic UITableView inside static cell

I’ve read some threads regarding static & dynamic cells incompatibility, but I’m wondering if there’s some workaround for my case.

I have a static table (handled by a UITableViewController). I put a dynamic table inside one of its cells. Delegate & datasource is the UITableViewController for both tables, and it works pretty well as long as the number of rows for the inner dynamic table is less than the static one. When the dynamic table has more cells than the static one, I get an index i beyond bounds exception.

  • Custom UIPopoverBackgroundView: no drop shadow
  • IPhone simple phone number validation
  • Objective C Introspection/Reflection
  • How to draw a line in the simplest way in swift
  • Building a chess board game in Sprite Kit using Swift?
  • use prepareForSegue for button
  • I’m assuming somehow the total number of cells is statically defined and shared by both tables, but can’t really understand what’s exactly happening. Anybody faced a similar problem and found a workaround?

    EDIT

    Here’s my numberOfRowsInSection method. In every method of my delegate/datasource, I check wether the calling table is the dynamic one (_tableOptions) or the static (in which case, I call to parent’s method).

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (tableView == _tableOptions) {
            // I return here the number of rows for the dynamic inner table
        } else {
            return [super tableView:tableView numberOfRowsInSection:section];
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (tableView == _tableOptions) {
            static NSString *simpleTableIdentifier = @"cellOptions";
    
            CellOptions *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
            // Doing some stuff with my cell... 
    
            return cell;
    
        } else {
            return [super tableView:tableView cellForRowAtIndexPath:indexPath];
        }
    }
    

    Solutions Collect From Internet About “Dynamic UITableView inside static cell”

    This is not a question of compatibility. If you opt to implement programmatic control functions for static UITableViews then you have to be sure that you’re not conflicting with how they’re defined in the storyboard.

    For example if you implement this function for a UITableView with static cells

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
         return 5;
    }
    

    but you’ve only added 3 static cells for the given view and controller in the storyboard then Xcode cannot simply create 2 more static cells from nothing. I think that the closest thing to a workaround for you is to add another UITableViewController for the table with dynamic cells. You can instantiate the new controller in the current controller that you’re using without presenting its view on the screen. Then you can assign the UITableView that has dynamic cells to be the new UITableViewController’s tableView property. Likewise, assign the delegate and datasource properties of the UITableView to be the new controller.

    EDIT: After seeing the code I do know of one possible workaround. You can leverage the number of sections to trick the UITableViewController into doing what you want.

    You can use the code below to add arbitrary number of cells to the dynamic view because you’re adding the cells to the dynamic table’s second section but at the same time setting the number of cells in the static table’s second section to 0. The key is that you MUST add the max number of cells to the second section of the static table in the storyboard. These will be dummy cells that are never displayed.

    In the following image you can see that I set the second section of my static table to be 10 cells and in the code I am able to return up to 10 cells for the dynamic tableview.

    enter image description here

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        if (tableView == dynamic)
        {
            return 2;
        }
        else
        {
            return 1;
        }
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (tableView == dynamic)
        {
            if (section == 1)
            {
                return 10;
            }
        }
        else
        {
            if (section == 0)
            {
                return [super tableView:tableView numberOfRowsInSection:section];
            }
        }
        return 0;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView == dynamic) {
            static NSString *simpleTableIdentifier = @"sample";
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
            if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
            }
    
            [cell.textLabel setText:[NSString stringWithFormat:@"cell %d",indexPath.row]];
    
            // Doing some stuff with my cell...
    
            return cell;
        }
        else
        {
            return [super tableView:tableView cellForRowAtIndexPath:indexPath];
        }
    }
    

    You can remove the section headers by implementing this function to clean it up:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 0;
    }
    

    After removing the headers for the sections you get exactly what you’re trying to accomplish. There are 10 cells in the dynamic table inside of the third cell of the static table.

    enter image description here