如何使用静态和dynamic单元实现UITableView – IOS

我创build了一个完全由四个不同的分组组成的UITableView。 每个部分/组具有不同数量的行。 我已经为底部三组静态地添加了必要数量的行和文本框。 但是,我不知道顶部部分/组需要多less行。 我已经build立了顶级组,只是在.xib中有一个单元格,但每次加载视图时,我都要检查我需要的单元格数量,然后dynamic地将所需数量的行添加到顶部部分。

我浏览过StackOverflow,看到有人正在讨论insertRowsAtIndexPaths方法,但是我一直无法使它工作。

[settingsPageTableView beginUpdates]; [settingsPageTableView insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationAutomatic]; [settingsPageTableView endUpdates]; 

我是否需要在insertRowsAtIndexPaths方法中放置一些特定的代码? 我传递了一个NSMutableArray,如下所示:

  NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:1 inSection:0]; NSMutableArray *tempArray=[NSMutableArray array]; [tempArray addObject:indexPath0]; 

如果任何人有任何知识如何将一个UITableView中的静态和dynamic单元格相结合,将非常感激! 对于熟悉iPhone上的常规设置的蓝牙页面的人来说,这正是我期待的。 它具有可变数量的设备以及在顶部包含UISwitch的静态单元。 有谁知道如何做到这一点?

谢谢一堆!

我想你应该dynamic地做这一切,只是因为你在蓝牙页面上描述的单元总是在那里并不意味着它是一个静态的单元格,只是总是dynamic地创build。

所以这里是我刚刚发现的解决方法。 我知道肯定永远不会超过7个单元格,我将永远需要UITable视图的顶部。 那么,我可以在.xib文件中静态设置这7个。 从那里我只能使用这个显示我需要的具体数字:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch(section) { case 0: //Do a bunch of checking here on how many i actually need. Then, I will just return the required amount. As long as it is less than the number of cells I have statically placed in my .xib, everything works good. return 4; case 1: return 3; case 2: return 1; case 3: return 2; case 4: return 3; default: return 0; } //Every case but the first always needs the same amount of cells, so those are no problem. } 

最好和最简单的方法:

1)将容器视图放置在dynamic表格视图的标题中2)将静态表格视图分配给该容器并取消“滚动已启用”