2

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.

3 Answers 3

2

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!

4
  • thx!! idd i will need more than one type of tables view. I need for video, photos, plain text and some other stuff. Now, i can create tables in one view, if i make 150k tables, how can i scroll the view? its not a good practices right? my problem is that i need something similar to hitpad app. thx in advance
    – pata
    Nov 9, 2011 at 20:45
  • You're fine with the scroll view. What you need to do is keep track of how many table views you've added and set your content size accordingly. For example, prior to the loop where the tables are initialized, set an integer to 0. Inside the loop, when each is created, set the integer to += table.frame.origin.y. After the loop, set the content view for the scroll view, using the integer as the parameter for the height! Nov 9, 2011 at 20:55
  • dude, just what i needed :D just need to create custom cells and im dune with this. thx thx thx thx thx thx thx !!!
    – pata
    Nov 11, 2011 at 12:49
  • I believe using different controllers as delegates is a better solution. Subclassing makes it more vague and increase what's called "intellectual distance" in Objectville :) Oct 12, 2013 at 15:38
0

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.

1
  • To be more explicit, my boss loves hitpad app and the whey that they show their main view. Any idea how they do that effect?
    – pata
    Nov 9, 2011 at 15:00
0

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
1
  • this helped, how can i scroll the main view where i have this 2 tables ?
    – pata
    Nov 9, 2011 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.