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 (hand
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.
- (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.