这不是兼容性问题。如果您选择为静态 UITableView 实现编程控制功能,那么您必须确保不会与它们在故事板中的定义方式发生冲突。
例如,如果您为具有静态单元格的 UITableView 实现此函数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
但是您只为情节提要中的给定视图和控制器添加了 3 个静态单元格,那么 Xcode 无法简单地从无到有再创建 2 个静态单元格。我认为最接近解决方法的方法是为带有动态单元格的表格添加另一个 UITableViewController 。您可以在当前使用的控制器中实例化新控制器,而无需在屏幕上显示其视图。然后,您可以将具有动态单元格的 UITableView 指定为新的 UITableViewController 的 tableView 属性。同样,将 UITableView 的 delegate 和 datasource 属性指定为新控制器。
EDIT:看到代码后,我确实知道一种可能的解决方法。您可以利用部分的数量来欺骗 UITableViewController 执行您想要的操作。
您可以使用下面的代码将任意数量的单元格添加到动态视图,因为您要将单元格添加到动态表的第二部分,但同时将静态表的第二部分中的单元格数量设置为 0。那是你吗MUST将最大单元格数添加到情节提要中静态表的第二部分。这些将是永远不会显示的虚拟单元格。
在下图中,您可以看到我将静态表的第二部分设置为 10 个单元格,在代码中我最多可以返回 10 个单元格dynamic
表格视图。
- (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];
}
}
您可以通过实现此函数来删除节标题来清理它:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
删除各部分的标题后,您将得到您想要完成的任务。细胞内有10个dynamic
静态表的第三个单元格内的表。