Bellots
Bellots

Reputation: 1773

Troubles with datasource and tableviews

I've got an UIViewController with 2 tableviews:

1- the main tableview, which is shown in the whole view controller

2- the second viewcontroller, which is loaded in a popup view.

The second tableview is shown on swiping a cell of main tableview.

Depending on which cell is swiped, there are different data in popup view.

I've already loaded the whole data in viewdidload method and stored everything in nsmutablearray, so are ready to be loaded.

My problem is that I don't know how to work with tableview's DataSource, in my project i linked both tableview's datasource to file's owner, but in this way it loads the numberofrows from the main view, and it doesn't take the correct count which should have the second tableview.

So, if in main tableview i have for example 3 elements, and in the second tableview it should load 5 elements, it gives me an error, ('NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index…).

I think that i should link my tableviews to different datasource, even if i really don't know.

I hope to be clear, if you need more info, or a better description, ask me and i'll do it for you.

Upvotes: 1

Views: 73

Answers (6)

pro_metedor
pro_metedor

Reputation: 1176

You can do it both ways:

  1. Make separate classes as data source for separate tables. Instantiate their objects as datasources for tables and bind them at viewDidLoad method in proper view controller.

  2. Make one datasource for 2 tables which I don't recommend as it is not comply with proper OOAD. You'll have tight coupling this way between view controller and the table which can be cause of trouble in the near future.

You have method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

in which you can distinguish to which table you are referring:

if (tableView == mainTableView) {
     // initialize cell for main table view
}
if (tableView == secondTableView) {
    // intialize cell for second table view
}

Upvotes: 0

Nameet
Nameet

Reputation: 650

You will need to check for the tableview in all your delegate and datasource methods as follows:

if (tableView == mainTable)
{
    // code for your main table
}
else if (tableView == popupTable)
{
    // code for your popup table
}

You do same for 2 or more table views. Hope this helps.

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

The same instance of a view controller serving as data source/delegate for two tables is technically possible but has a number of traps. One of these is: to which of the two tables does self.view refer?

However, most of the data source protocols hand down a reference to the table. You could use that rerference to determine which table acutally sends the request. But not all methods of the protocol inclulde a reference to the table. And that is where it starts getting tricky.

You are far better off with two distinctive view controllers, one for each table. Establish a protocol between them so that the main view controller can hand down the data to the one in the popup window so that the popup can initialize/load it self with the proper data and can refresh its view when ever the data changes.

Upvotes: 0

Guillaume Algis
Guillaume Algis

Reputation: 11016

You can use one View Controller as an unique data source for multiple table view, but you'll need to check which table view is requesting data using the tableView arguments of the UITableViewDataSource methods.

For example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.mainTableView)
    {
        // code for your main table view
    }
    else
    {
        // code for your popup table view
    }
}

Upvotes: 0

Prashant Nikam
Prashant Nikam

Reputation: 2251

No Problem

you just set tags to your tables.

[tableView1 setTag:1];
[tableView2 setTag:2];

and then

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

     if (tableView.tag == 1) {
        return 4;
     }
     else if (tableView.tag == 2)
     {
        return 5;
     }
}

do similar thing all data source method

Upvotes: 0

Apurv
Apurv

Reputation: 17186

In the delegate method, you should compare the tableview.

See the example,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == maintableView)
          return VALUE_FOR_MAIN_TABLE_DATA;
    else  
          return VALUE_FOR_POP_TABLE_DATA;  
}

Upvotes: 2

Related Questions