dynamicUITableView里面的静态单元格

我已经阅读了一些有关静态和dynamic单元格不兼容的主题,但是我想知道是否有一些解决scheme适用于我的情况。

我有一个静态表(由UITableViewController处理)。 我在其中一个单元格中放置了一个dynamic表格。 委托和数据源是两个表的UITableViewController ,只要内部dynamic表的行数小于静态表的行数,它就工作得很好。 当dynamic表比静态表有更多的单元格时,我得到一个index i beyond boundsexception的index i beyond bounds

我假设细胞的总数是静态定义和两个表共享,但不能真正了解到底发生了什么。 有人遇到类似的问题,并find解决办法?

编辑

这是我的numberOfRowsInSection方法。 在我的代理/数据源的每个方法中,我检查调用表是dynamic的(_tableOptions)还是静态的(在这种情况下,我调用父类的方法)。

 - (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]; } } 

这不是一个兼容性的问题。 如果您select为静态UITableView实现编程控制function,那么您必须确保您与故事板中的定义方式没有冲突。

例如,如果您为具有静态单元格的UITableView实现此函数

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

但是你只在storyboard中为给定的视图和控制器添加了3个静态单元,那么Xcode不能简单地创build2个静态单元。 我认为最接近你的解决方法是为dynamic单元格的表添加另一个UITableViewController。 您可以在当前控制器中实例化新控制器,而不必在屏幕上显示其视图。 然后你可以分配具有dynamic单元格的UITableView作为新的UITableViewController的tableView属性。 同样,将UITableView的委托和数据源属性分配为新的控制器。

编辑:看到代码后,我知道一个可能的解决方法。 你可以利用一些部分来欺骗UITableViewController来做你想做的事情。

您可以使用下面的代码将任意数量的单元格添加到dynamic视图中,因为您将单元格添加到dynamic表格的第二部分,但同时将静态表格的第二部分中的单元格数设置为0。是你必须添加最大数量的单元格到故事板中静态表格的第二部分。 这些将是从不显示的虚拟单元格。

在下面的图片中,您可以看到我将静态表的第二部分设置为10个单元格,并且在代码中,我可以返回最多10个单元格作为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]; } } 

你可以通过实现这个function来清除节头信息:

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

删除部分的标题后,你得到了你想要完成的。 静态表的第三个单元格内的dynamic表中有10个单元格。

在这里输入图像说明