ipad如何在一个视图上以编程方式创建多个表 [英] ipad how to create programmatically multiple tables on one view

查看:109
本文介绍了ipad如何在一个视图上以编程方式创建多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个iPad应用程序,我必须在一个视图中显示多个表(没有网格,只有1个多列的列)。这必须以编程方式完成,因为在后台有人会设置该数量的表。

i need to create an iPad app where i have to show multiple tables(no grid, just 1 collumn with multiple rows) in one single view. this have to be done programmatically because in a back-office someone is going to set that number of tables.

视图将有一个滚动,所以我可以看到所有这些。

the view will have a scroll so i can see all of them.

可以这样做吗?

有人可以提供我的一些代码或链接到一些教程如何在一个视图中创建N个表,在我想要的时候定位它们。

can someone provide my some code or link to some tutorial about how to create a N number of tables in one view positioning them whenever i want.

推荐答案

这绝对可以做到。

可能最简单的方法是创建UITableView的子类,这样你创建的每个TableView都可以为其委托和数据源设置一个唯一的处理程序,ala:

Probably the easiest way you can do this is to subclass UITableView, so that each TableView you create can have a unique handler for its delegate and datasource, ala:

DynamicTableView.h

DynamicTableView.h

@interface DynamicTableView : UITableView <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *items;
}

@end

DynamicTableView.m

DynamicTableView.m

#import "DynamicTableView.h"

@implementation DynamicTableView

-(id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self == [super initWithFrame:frame style:style]) {
        items = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"%i", [NSDate timeIntervalSinceReferenceDate]],
                 [NSString stringWithFormat:@"%i", [NSDate timeIntervalSinceReferenceDate]], nil];
    }

    return self;
}

-(void) dealloc {
    [items release];

    [super dealloc];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [items count];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [items objectAtIndex:indexPath.row];

    return cell;
}

@end

这是一个非常简单的实现,当它初始化时,用两个时间戳填充其数据源(items数组)。使用它就像这样简单:

This is a very simple implementation, that when it's initialized fills its datasource (the items array) with two timestamps. Using it is as simple as something like:

for (int i = 0; i < 4; i++) {
    DynamicTableView *table = [[[DynamicTableView alloc] initWithFrame:CGRectMake(10, (i * 100) + 10, 200, 50) style:UITableViewStylePlain] autorelease];
    [table setDelegate:table];
    [table setDataSource:table];

    [self.view addSubview:table];
}

修改DynamicTableView以接受您想要的任何数据源及其显示方式。

Modify the DynamicTableView to accept whatever data source you want and how it is displayed.

希望有所帮助!

这篇关于ipad如何在一个视图上以编程方式创建多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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