0

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

Sankar Chandra Bose
  • 377
  • 1
  • 12
  • 27

4 Answers4

2
-(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];        
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
dhaval rupani
  • 542
  • 5
  • 8
1

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.

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
0

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
Community
  • 1
  • 1
kovpas
  • 9,553
  • 6
  • 40
  • 44
0

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.

lancegoh
  • 624
  • 1
  • 6
  • 16