Reputation: 989
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.
Can this be done right ?
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.
Upvotes: 2
Views: 5079
Reputation: 3980
I hope that next code could be a starting point for you:
@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
Upvotes: 0
Reputation: 1307
This absolutely can be done.
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
@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
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];
}
Modify the DynamicTableView to accept whatever data source you want and how it is displayed.
Hope that helps!
Upvotes: 2
Reputation: 75296
From your description, I'm assuming that you want to have an arbitrary number of tables, all of which are sitting on a single view which is itself scrollable (so that you scroll up or down to get to all of the tables). This is highly inadvisable in iOS, as the table view is itself a subclass of the scrollable view and you will have major problems getting the individual tables to scroll properly along with the "master" scrollable view.
Assuming this is what you're trying to do, you would be much better off using a single table view that is split into sections. Here is a good tutorial that shows how to do this.
Upvotes: 0