如何使用静态和动态单元实现UITableView - IOS [英] How to implement UITableView With Static and Dynamic Cells -- IOS

查看:162
本文介绍了如何使用静态和动态单元实现UITableView - IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a UITableView that is entirely composed of four different groupings. 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.

我已经看过StackOverflow,并且看到有人讨论了insertRowsAtIndexPaths方法,但是我无法让它工作。

I've looked around StackOverflow and have seen people discussing the insertRowsAtIndexPaths methods, but I haven't been able to get it to work.

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

我需要在insertRowsAtIndexPaths方法中放入一些特定的代码吗?我已经通过了一个NSMutableArray:

Do I need to put some specific code inside of the insertRowsAtIndexPaths methods? I've passed in an NSMutableArray as so:

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

如果任何人知道如何在一个UITableView中组合静态和动态单元格,那将是非常不胜感激!对于熟悉iPhone上一般设置的蓝牙页面的任何人来说,这正是我正在寻找的。它具有可变数量的设备,以及包含顶部的UISwitch的静态单元。有没有人知道如何完成这个?

If anyone has any knowledge of how to combine static and dynamic cells in one UITableView, it would be extremely appreciated! 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. It has a variable number of devices show up as well as a static cell containing a UISwitch at the top. Does anyone know how to accomplish this?

感谢一堆!

推荐答案

p>所以这里是我刚才想出来的解决方法。我知道肯定会永远不会有超过7个细胞,我会需要的UITable视图的顶部。那么,我可以在.xib文件中静态地设置这些7。从那里我只能显示我需要使用的具体号码:

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. So then, I am able to just set up those 7 statically in the .xib file. 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.
}

这篇关于如何使用静态和动态单元实现UITableView - IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆