Base UIViewController implements UITableViewDelegate and child view controller as well causing an Exception

Hey guys,


I have created a base UIViewController in my app which contains some shared functionality between other child controllers. The problem is that my base view controller implements the UITableViewDelegate and UITableViewDataSource, so if any of the child view controllers implements the same UITableViewDelegate and UITableViewDataSource raises an exception saying that I can't self as a subview of self.


I have tried different possible solutions and discover that the issue is actually the fact of having two table view. One is being inherited from the base controller and the other is in the child controller as soon as I remove the child controller table view everything works fine.


Also, I'm doing the layout in Storyboards. 😟


Any ideas?


Thanks!

Answered by JVDev17 in 174083022

So, at the end the issue was exactly as explained in the Exeception. I was trying to add the same view as a subview within my table view header view.


[view addSubview:view];


It was a hard to find bug, but thanks to pair programming the issue was resolve. Just be careful with this type of bugs.

🙂

I have tried following solution. It may help you...


1. My base class BaseViewController.h code...


#import <UIKit/UIKit.h>
@interface BaseViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) IBOutlet UITableView *tblBaseClass;
@end

2. My child class ViewController.h code


#import <UIKit/UIKit.h>
#import "BaseViewController.h"
@interface ViewController : BaseViewController
{
    IBOutlet UITableView *tblChildClass;
}
@end


3. My child class ViewController.m code


#pragma mark - Tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSInteger count;

    if (tableView == tblChildClass) {

        count = 2;
    }
    else if (tableView == self.tblBaseClass) {

        count = 4;
    }
    return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell;/
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
    /

    NSString *strCellText;

    if (tableView == tblChildClass) {

        strCellText = [NSString stringWithFormat:@"Child class table row : %ld", (long)indexPath.row];
    }
    else if (tableView == self.tblBaseClass) {

        strCellText = [NSString stringWithFormat:@"Base class table row : %ld", (long)indexPath.row];
    }

    cell.textLabel.text = strCellText;

    return cell;
}



In storyboard, add two table view in your child view controller (Here ViewController).

Set IBOutlet to child view controller, also set data source and delegate to child view controller

Here I am not able to show screenshot, otherwise I would like to post for better understanding…sorry… 😟

Thanks for your answer. Do you know the reason for why using ivars instead of property for the child controller?

Accepted Answer

So, at the end the issue was exactly as explained in the Exeception. I was trying to add the same view as a subview within my table view header view.


[view addSubview:view];


It was a hard to find bug, but thanks to pair programming the issue was resolve. Just be careful with this type of bugs.

🙂

Base UIViewController implements UITableViewDelegate and child view controller as well causing an Exception
 
 
Q