iPad的如何在一个视图上编程创build多个表

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

该视图将有一个滚动,所以我可以看到所有的人。

这可以做对吗?

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

这绝对可以做到。

也许最简单的方法就是创buildUITableView的子类,这样你创build的每个TableView都可以拥有一个唯一的代理和数据源处理器,ala:

DynamicTableView.h

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

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数组)。 使用它就像这样简单:

 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以接受所需的任何数据源以及如何显示它。

希望有所帮助!

从你的描述中,我假设你想拥有一个任意数量的表,所有这些表都坐在一个单独的视图上,这个视图本身是可以滚动的(所以你可以向上或向下滚动以获得所有的表)。 这在iOS中是非常不合适的,因为表视图本身就是可滚动视图的子类,并且在使用“主”滚动视图时,您将会遇到重大问题,使得各个表能够正确滚动。

假设这是你正在做的事情,那么使用一个拆分成部分的表格视图会更好。 这是一个很好的教程 ,展示了如何做到这一点。

我希望下一个代码能成为你的起点:

 @interface MyController : UIViewController <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, retain) UITableView *table1; @property (nonatomic, retain) UITableView *table2; @end @implementation MyController @synthesize table1 = _table1, table2 = _table2; - (void)viewDidLoad { [super viewDidLoad]; CGRect table1Rect = CGRectMake(0, 0, 200, 300); UITableView *table1 = [[UITableView alloc] initWithFrame:table1Rect style:UITableViewStyleGrouped]; table1.delegate = self; table1.dataSource = self; [self.view addSubview:table1]; self.table1 = table1; [table1 release]; CGRect table2Rect = CGRectMake(200, 0, 200, 300); UITableView *table2 = [[UITableView alloc] initWithFrame:table2Rect style:UITableViewStyleGrouped]; table2.delegate = self; table2.dataSource = self; [self.view addSubview:table2]; self.table2 = table2; [table2 release]; } - (void)viewDidUnload { self.table1 = nil; self.table2 = nil; } - (void)dealloc { self.table1 = nil; self.table2 = nil; [super dealloc]; } @end 

如果你在viewController中放置2个表格,并在同一个类中find你的委托和数据源方法,那么要么为你的tableView设置标签,要么给它们添加sockets,并且代理和数据源的每个函数都可以作为调用者参考在每个方法中(你可以看到它们为(UITableView *)tableView):

if(tableView == table1)或if(tableView.tag == 1){} else {}

2)创build单独的NSObject类(比如TableClassSource),并使用UITableViewDelegate和UITableViewDataSource进行设置,并在此处进行编码。 并在你的ViewController中创build这个类的Object,并将你的委托和表的数据源设置为这个对象

TableClassSource * obj = [[TableClassSource alloc] init]; table1.dataSource = OBJ; table1.delegate = OBJ;

你可以在TableClassSource中创build一个类似loadMethod的方法,如果你需要每次更新表,就调用它来重新加载

或者更多的细节你可以看到并从下载示例项目

在一个View-Part-1中处理多个表格

在一个View-Part-2中处理多个表格