动态生成UITableViews [英] Dynamically Generate UITableViews

查看:39
本文介绍了动态生成UITableViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在打电话给Web服务以找回策略.用户可以有多个策略,并且这些策略要有自己的表(请注意,表中没有自己的部分).这些表中的行数都未知,这取决于对策略的修改数量加上策略本身.因此,我需要为此动态生成表到UIScrollView上.有人知道我该怎么做吗?如果您不了解我,请询问.

Ok I am making a call to a web service to get policies back. The user can have multiple policies, and these policies are to have there own table (Please note not there own section in a table). These tables can have an unknown number of rows in them all depending on the number of amendments to the policy plus the policy itself. So I need to dynamically generate the tables for this onto a UIScrollView. Does anyone know how I can do this? If you don't understand me then please ask.

推荐答案

您可以通过编程方式创建UITableViews

You can create UITableViews programmatically with

UITableView *tableView = [[UITableView alloc] init];

然后将视图控制器设置为表视图的委托和数据源:

then set the view controller to be the delegate and data source of the table view:

tableView.delegate = self;
tableView.dataSource = self;

并将表格视图添加到滚动视图中,例如:

and add the table view to your scroll view like such:

tableView.frame = CGRectMake(0, 0, 320, 500); //use whatever frame you want here
[self.scrollView addSubview:tableView];

并且为了跟踪表视图,可以使用与每个视图相关联的标记.最好将它们定义为.m文件顶部的常量.您可以适当地命名它们,以便您知道表视图应采用的策略.例如,在您的.m文件顶部:

and in order to keep track of the table views, you can use the tag associated with each one. These are probably best defined as constants at the top of your .m file. You can name them appropriately so you know what policy the table view is supposed to be for. For example, at the top of your .m file:

#define kFirstPolicyTableViewTag 1

,然后在创建表视图时:

and then when creating the table view:

tableView.tag = kFirstPolicyTableViewTag;

然后,在处理任何委托或数据源方法时,请检查该标记,以使您知道要处理的是哪个标记.例如:

then, when dealing with any of the delegate or data source methods, check the tag so you know which one you are dealing with. For example:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (tableView.tag) {
        case kFirstPolicyTableViewTag: {
            //return the number of rows for that tag
        }
    }
}

这篇关于动态生成UITableViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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