Rajan Balana
Rajan Balana

Reputation: 3815

Overriding of Tableview Datasource/Delegate Methods

I have one parent class with one tableview.

That class is the delegate and datasource of that tableview as well.

Now I subclassed (derived) that class and made a child class.

I have one tableview in child class too.

Then I defined delegate and datasource functions in that child class, but it overrides parent class tableview data source/delegate methods.

But I want both of them to be separate.

However my Requirement is as Follows :

I want to retain a search bar and side button, on the top of all the viewControllers that search bar includes , a recent searches terms table underneath that.

So i thought of defining parent class for that and subclass other viewControllers from that class.

Am i doing it the right way ?

Upvotes: 1

Views: 4233

Answers (2)

Hermann Klecker
Hermann Klecker

Reputation: 14068

I assume you are talking about a view controller class. If I understood you right, then you are about to mess it up. Delegation is a way to avoid subclassing. Of course you can subclass the delegate - no problem. But you want a table view in the super class that owns a table in its view. And you want a subclass that has another table in its view plus the table that the superclass owns.

That is not impossible. But from your subclass' point of view, your subclass owns two table views. Even that is possible. Your view controller is the delegate of two tables (regardless of where in the view hierarchy they are declared and instanciated). When you now override the delegate and data source methods theny your subclass must either:

  1. Determine which table it is dealing with/being called from. And then serve both tables appropriately.
  2. Determine wich table it is dealing with/being called from. And then serve "its own" table appropriately and calls [super sameMehtod:withSamePamaters] to ensure that the superclas can still provide the data and server as delegate.

Which of both is smarter depends on the context and what you are about to achieve in detail.

A way of determinnig which table's delegate was called can be done by tagging the table views (do not use 0 as tag) or by comparing the tableView parameter of the delegate method with the corresponding properties (IBOutlets in this case). (In other cases you can compare the sender parameter with the IBOutlets. But tagging is probably easier to understand when reading the code later.)

Let's look at an example of the UITableViewDataSourceDelegat:

Your superclass implements:

@interface MySuperTableViewController:UITableViewController <UITableViewDelegate>

// There will be something in here.
// But it inherits self.tableView from UITableViewController anyway. We leave it with that. 

@end

@implementation MySuperTableViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  // This method creates or re-uses a cell object and sets its properties accordingly.

}

@end

And your subclass:

@interface MySubTableViewController : MySuperTableViewController // no need to declare the delegate here, it is inherited anyway

@property (weak, nonatomic) IBOutlet UITableView  *mySecondTableView;  // self.table will be used by the superclass already. 

@end

@implementation MySubTableViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView == self.table) { // This call refers to the one talbe that is managed by super
     return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}

   // This method now creates or re-uses a cell object and sets its properties accordingly.
   // You may want to check wether tableView == self.mySecondTableView etc.
}

@end

(This comes from scratch, not syntax checked etc. Do not expect this to run properly right away :)

BUT ... please re-consider your class structure. I am afraid you are getting lost in some rather unlogical class hierarchy. There is nothing wrong with having two talbes managed by a common view controller even without this subclassing-thing. And there is nothing wrong with using multiple tables in a view where each of the tables has its own delegate (can be a view controller). Since iOS 5 (or was it introduces with 6) we can use the UIContainerView for that purpose and nicely build it up in IB/storyboard.

Upvotes: 2

Ravindhiran
Ravindhiran

Reputation: 5384

try this,

ViewController.h

    IBOutlet UITableView *firstTable;
    IBOutlet UITableView *secondTable;

ViewController.m

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
   if (tableView == firstTable) {
       return 1;
    } 
   else if(tableView == secondTable)
   {
      return 1;
   }
return 0;
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (tableView == firstTable) {
    return [arrItems count];
} else if(tableView == secondTable)
{
    return [arrData count];
}
 return 0;
}

etc etc ....

Upvotes: 0

Related Questions