pithhelmet
pithhelmet

Reputation: 2282

Load three UITableViews from different datasources

I have three uiTableViews on a view.

I have created three different NSMutableArray loaded with different data.

I need to place one of the NSMutableArray as the datasource for one of the UITableView.

I am able to assign all three UITableViews datasource through the viewDidLoad of the form.

But what I really need to do, is assign each of the UITableView datasource to a different NSMutableArray.

How can I perform this task?

thanks
tony

Upvotes: 0

Views: 331

Answers (1)

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

If all three UITableViews share the same datasource object (the object that holds all three of your arrays), then just use if statements to differentiate between the table views asking for the data:

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section
{
    // If table 1 is asking, give it table 1 data...
    if (tableView == myTableView1)
    {
        // Assume all sections have 3 rows each for
        // purposes of simple demonstration...
        return [dataSourceForTable1 count];
    }

    // If table 2 is asking, give it table 2 data...
    if (tableView == myTableView2)
    {
        // Assume all sections have 3 rows each for
        // purposes of simple demonstration...
        return [dataSourceForTable2 count];
    }

    // If table 3 is asking, give it table 3 data...
    if (tableView == myTableView3)
    {
        // Assume all sections have 3 rows each for
        // purposes of simple demonstration...
        return [dataSourceForTable3 count];
    }

   // The compiler will complain if we don't have
   // a final return since it's possible none of the 
   // if statements will be true ...
   return 0;
}

Upvotes: 2

Related Questions