Sankar Chandra Bose
Sankar Chandra Bose

Reputation: 377

UITableView in iPhone

Guys, i am a newbie in iPhone but not to Programming, my doubts are,

  1. Is it possible to add more than one UITableView in the same screen? ,if so please provide me with sample code / resource where i can find..

  2. The Second UITableView has to be changed accordingly based on the selection in the First UITableView.

Thanks in Advance

Upvotes: 0

Views: 206

Answers (4)

dhaval rupani
dhaval rupani

Reputation: 542

-(void)loadView
{   
    // create and configure the view
    CGRect cgRct = CGRectMake(0, 10, 320, 100); //define size and position of view
    myView = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
    myView.autoresizesSubviews = YES; //allow it to tweak size of elements in view
    self.view = myView; //set view property of controller to the newly created view
    UITableView * tableView = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
    tableView.editing = YES;
    tableView.dataSource = self;
    tableView.delegate = self;

    cgRct = CGRectMake(0, 120, 320, 100); //define size and position of view
    UITableView * tableView1 = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
    tableView1.editing = YES;
    tableView1.dataSource = self;
    tableView1.delegate = self;

    cgRct = CGRectMake(0, 230, 320, 100); //define size and position of view
    UITableView * tableView2 = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
    tableView2.editing = YES;
    tableView2.dataSource = self;
    tableView2.delegate = self;

    [self.view addSubview:tableView];
    [self.view addSubview:tableView1];
    [self.view addSubview:tableView2];        
}

Upvotes: 2

lancegoh
lancegoh

Reputation: 692

You could consider using Section in uiTableview.

This will helps with memory foot print compares to loading in 2 UiTableView.

First set the number of section on load and should a certain condition are met. Set the section to 2, call [tableview reloadData]

This should load the - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; where you set the number of section to 2.

Within - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; you will set the numbers of rows in each section using

if (section == 0){ do something} else { do something else}

This is assuming you have 2 sections.

Finally within - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;, you should use the same if else to check which section and load the respective data in.

Upvotes: 0

Shaggy Frog
Shaggy Frog

Reputation: 27601

It's possible, but in the sense you're describing is probably a UI convention violation. You should be presenting one UITableView per screen, where tapping a row on the first "drills down" into a second UITableView -- like a hierarchy.

Upvotes: 1

kovpas
kovpas

Reputation: 9593

Here's a sample code. But the better approach is, yes, to have different delegates/datasources for both tables.

To change table2 contents depending on a table1 selection, you may just use [table2 reloadData] in a

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 0

Related Questions