Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I never figured out what was causing the problem, but instead I subclassed my TableViewCell (the one that contains the dynamic table) and let it implement the neccessary protocols and assigned the table views delegate and datasource to it, instead of the view controller of the static table view. That works perfectly fine and is probably better design anyway.</p> <p>Here is some example code:</p> <pre><code>@interface CellWithTableInside : UITableViewCell &lt;UITableViewDelegate, UITableViewDataSource&gt; @property (nonatomic, strong) UITableView* tableViewInCell; @property (nonatomic, strong) NSArray* cellData; @end @implementation CellWithTableInside @synthesize tableViewInCell; @synthesize cellData = _cellData; - (void)setCellData:(NSArray *)cellData { _cellData = cellData; [tableViewInCell reloadData]; } - (void)setFrame:(CGRect)frame { [super setFrame:frame]; [tableViewInCell setFrame:self.bounds]; } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self setupTableView]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self setupTableView]; } return self; } - (void)setupTableView { tableViewInCell = [[UITableView alloc] initWithFrame:self.bounds]; [tableViewInCell setDataSource:self]; [tableViewInCell setDelegate:self]; [self addSubview:tableViewInCell]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.cellData.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"NestedCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text = [self.cellData objectAtIndex:indexPath.row]; return cell; } @end </code></pre> <p>You can use <code>CellWithTableInside</code> like this:</p> <pre><code>@interface HostingTableViewController : UITableViewController @property (nonatomic, strong) NSArray* tableData; @end @implementation HostingTableViewController @synthesize tableData; - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } - (void)viewDidLoad { tableData = [NSArray arrayWithObjects: [NSArray arrayWithObjects:@"February", nil], [NSArray arrayWithObjects:@"Apple", @"Banana", nil], [NSArray arrayWithObjects:@"John", @"Peter", @"Edward", nil], [NSArray arrayWithObjects:@"Yellow", @"Green", @"Red", @"Blue", nil], nil]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return tableData.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44 * [(NSArray*)[tableData objectAtIndex:indexPath.row] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellWithTableInside"; CellWithTableInside *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CellWithTableInside alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } [cell setCellData:[tableData objectAtIndex:indexPath.row]]; return cell; } @end </code></pre> <p>This works for both, dynamic or static table views.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload