【发布时间】:2011-11-09 14:03:31
【问题描述】:
我需要创建一个 iPad 应用程序,我必须在一个视图中显示多个表格(没有网格,只有 1 个多行列)。这必须以编程方式完成,因为在后台有人要设置该数量的表。
视图会有一个滚动条,所以我可以看到所有这些。
这可以做对吗?
有人可以提供我的一些代码或链接到一些关于如何在一个视图中创建 N 数量表的教程,以便随时定位它们。
【问题讨论】:
标签: ios ipad uitableview
我需要创建一个 iPad 应用程序,我必须在一个视图中显示多个表格(没有网格,只有 1 个多行列)。这必须以编程方式完成,因为在后台有人要设置该数量的表。
视图会有一个滚动条,所以我可以看到所有这些。
这可以做对吗?
有人可以提供我的一些代码或链接到一些关于如何在一个视图中创建 N 数量表的教程,以便随时定位它们。
【问题讨论】:
标签: ios ipad uitableview
这绝对可以做到。
您可以做到这一点的最简单的方法可能是继承 UITableView,这样您创建的每个 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 中是非常不可取的,因为表格视图本身是可滚动视图的子类,您将在让各个表格与“主”可滚动视图一起正确滚动时遇到重大问题。
假设这是您想要做的,您最好使用拆分为多个部分的单个表视图。这是a good tutorial,它显示了如何执行此操作。
【讨论】:
我希望下一个代码可以成为您的起点:
@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
【讨论】: