Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

ios - ipad how to create programmatically multiple tables on one view

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...