1
4

6 回答 6

2

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;  
}
于 2013-06-10T12:57:09.093 回答
0

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

于 2013-06-10T12:59:15.357 回答
0

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
    }
}
于 2013-06-10T12:59:15.707 回答
0

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.

于 2013-06-10T13:02:08.043 回答
0

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.

于 2013-06-10T13:03:54.960 回答
0

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
}
于 2013-06-10T13:04:29.143 回答