如何使用静态和动态单元实现UITableView - IOS

时间:2012-07-27 00:02:35

标签: ios dynamic uitableview

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

我查看了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中组合静态和动态单元格,那将非常感激!对于那些熟悉iPhone上一般设置的蓝牙页面的人来说,这正是我想要做的。它显示可变数量的设备以及顶部包含UISwitch的静态单元。有谁知道如何做到这一点?

非常感谢!

3 个答案:

答案 0 :(得分:3)

我认为你应该动态地完成所有操作,只是因为你在蓝牙页面上描述的单元格总是并不意味着它是一个静态单元格,只是总是动态创建。

答案 1 :(得分:2)

所以这是我刚才想到的解决方法。我确信UITable View的顶部部分永远不会超过7个单元格。那么,我可以在.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.
}

答案 2 :(得分:1)

最简单的方式:

1)将容器视图放置在动态TableView的标头中 2)将静态TableView分配给此Container并取消选中“Scrolling Enabled”