简体   繁体   English

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

[英]How to implement UITableView With Static and Dynamic Cells — IOS

I've created a UITableView that is entirely composed of four different groupings. 我创建了一个UITableView,它完全由四个不同的组组成。 Each section/group has a different number of rows. 每个部分/组具有不同数量的行。 I've statically added the necessary number of rows and textfields for the bottom three groups. 我已经为底部的三个组静态添加了必要数量的行和文本字段。 However, I do not know how many rows I will need for the top section/group. 但是,我不知道顶部/组将需要多少行。 I've set up the top group as just having one cell in the .xib, but each time the view loads I am going to have to check how many cells I need and then dynamically add the necessary number of rows to the top section. 我将顶级组设置为在.xib中只有一个单元格,但是每次视图加载时,我都必须检查我需要多少个单元格,然后将必要的行数动态添加到顶部。

I've looked around StackOverflow and have seen people discussing the insertRowsAtIndexPaths methods, but I haven't been able to get it to work. 我环顾了StackOverflow,看到人们在讨论insertRowsAtIndexPaths方法,但是我无法使其工作。

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

Do I need to put some specific code inside of the insertRowsAtIndexPaths methods? 我是否需要在insertRowsAtIndexPaths方法中放入一些特定的代码? I've passed in an NSMutableArray as so: 我已经通过了NSMutableArray:

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

If anyone has any knowledge of how to combine static and dynamic cells in one UITableView, it would be extremely appreciated! 如果有人知道如何在一个UITableView中组合静态和动态单元格,将不胜感激! For anyone that is familiar with the bluetooth page from the general settings on the iPhone, this is exactly what I'm looking to do. 对于任何熟悉iPhone常规设置中的蓝牙页面的人,这正是我想要做的。 It has a variable number of devices show up as well as a static cell containing a UISwitch at the top. 它具有可变数量的设备显示,以及在顶部包含UISwitch的静态单元格。 Does anyone know how to accomplish this? 有谁知道如何做到这一点?

Thanks a bunch! 谢谢一群!

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

So here's the workaround I just figured out. 所以这是我刚想出的解决方法。 I know for sure there will never be more than 7 cells that I would ever need for that top portion of the UITable View. 我知道,对于UITable View的顶部,我永远不会需要超过7个单元格。 So then, I am able to just set up those 7 statically in the .xib file. 这样,我就可以在.xib文件中静态设置这7个了。 From there I am able to only show the specific number that I need using this: 从那里,我只能使用此命令显示所需的具体号码:

- (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.
}

Best and Easiest Way: 最佳和最简单的方法:

1) Place Container View in the Header of a Dynamic TableView 2) Assign the Static TableView to this Container and Untick the "Scrolling Enabled" 1)将容器视图放置在动态TableView的标题中2)将静态TableView分配给此容器,然后取消选中“滚动启用”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM