静态和dynamic单元格的UITableView混合?

我知道你不能在单个UITableView混合静态和dynamic单元格types,但我想不出一个更好的方式来描述我的问题。

我有几个固定内容的预定义单元格,我也有一个未知数的dynamic内容单元格坐在中间。 所以我想我的桌子看起来像这样:

 Fixed Fixed Fixed Dynamic Dynamic Dynamic Dynamic Dynamic Fixed Fixed 

那么你究竟如何build议我在我的cellForRowAtIndexPath方法呢?

谢谢。

正如你所说,你不能混合静态和dynamic单元格。 但是,您可以将内容分解为与每个组对应的不同数据arrays。 然后将表分成不同的部分,并从cellForRowAtIndexPath:中正确的数组中加载数据。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"CELLID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; switch (indexPath.section) { case 0:{ cell.textLabel.text = self.arrayOfStaticThings1[indexPath.row]; }break; case 1:{ cell.textLabel.text = self.arrayOfDynamicThings[indexPath.row]; }break; case 2:{ cell.textLabel.text = self.arrayOfStaticThings2[indexPath.row]; }break; default: break; } return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { case 0:{ return self.arrayOfStaticThings1.count; }break; case 1:{ return self.arrayOfDynamicThings.count; }break; case 2:{ return self.arrayOfStaticThings2.count; }break; default: return 0; break; } } 

最好和最简单的方法

1)将两个容器视图放在页眉和dynamic表格视图的页脚中2)将静态表格视图分配给这些容器并取消“滚动已启用”

请在https://stackoverflow.com/a/22524085/1537178查看我的插图

在Storeboard中混合动态和静态表格

看看我的这个结构,完美地工作。 @firecast我没有在这个项目中使用约束,但是我根据容器的tableview高度设置了容器的高度,如下所示:

  ContainerViewController *containerVC = self.childViewControllers.firstObject; CGRect frame = self.containerView.frame; frame.origin.x = 0; frame.origin.y = 0; frame.size.width = self.tableView.frame.size.width; frame.size.height = containerVC.tableView.contentSize.height; self.containerView.frame = frame; [self.tableView setTableHeaderView:self.containerView]; 

希望这有助于,让我知道如果你有任何问题。