2

I have been faced with a puzzle I can't answer.

I am creating an app that has multiple UITableView within a single view. Each TableView will need to have different attributes from the other.

The UITableViewDelegate methods provide generic methods to configure UITableViews, but from what I am seeing, this will effect aLL TableViews within a View.

If I need to control the parameters for each single TableView - how would this be achieved?

Pfitz
  • 7,336
  • 4
  • 38
  • 51
Fritzables
  • 257
  • 1
  • 4
  • 10
  • Don't set your viewcontroller as delegate and datasource, instead create a nsobject subclass for each tableview and set them. It will be much more easier to manage and maintain. – Desdenova Apr 25 '13 at 08:21
  • duplicate: http://stackoverflow.com/q/1416372/1075405 – Groot Apr 25 '13 at 08:22

4 Answers4

3

That is not correct - you can configure everything with a UITableViewDelegate. Take for example:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

You can differentiate the tableviews via the tableView variable. For this you need to store a reference to the different tableviews (e.g. via a property).

@property (nonatomic, strong) UITableView *myFirstTableView;

Now you can do something like this:

if (tableView == self.myFirstTableView) {} else {}
Pfitz
  • 7,336
  • 4
  • 38
  • 51
0

for each of the delegate methods, its giving you which tableview it needs information. you can use that to identify which instance of the tableview requesting data..

use a tag to differentiate table views.

   tableview1.tag = 100;
    tableview2.tag = 200;
   // ... so on - if using interface builder you can set tag in there too :)

now in delegate.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableview.tag == 100)
{
// handle for tableview 1
}
else if (tableview.tag == 200)
{
// handle for tableview 2
}

}
nsuinteger
  • 1,503
  • 12
  • 21
0

All your tableView delegate methods starts with tableView as parameter.

Ex: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

This passes UITableView as parameter. Therefore, you can add switch-case or if statements to check for which tableView you want to modify data.

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
0

Instead of using the UIViewController as your tableView delegate, you could create a new class to act as tableView delegate. This class can then contain the parameters needed for the specific table that it is handling.

Inside the view controller you can then allocate one instance of this class per tableView and assign it as delegate.

Kobski
  • 1,636
  • 15
  • 27