Implement UITableView from different ControllerCla

2019-08-26 10:41发布

问题:

I'm trying to implement a TableView from a different (Table)ViewController into a Scrollview. I'm literally trying for hours now and I can't figure out the problem. The error I get is:

-[UISectionRowData numberOfSectionsInTableView:]: unrecognized selector sent to instance 0x687fdb0 2012-05-07 16:47:18.966 Test2[12212:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISectionRowData numberOfSectionsInTableView:]: unrecognized selector sent to instance 0x687fdb0'

This is the code Segment I'm working with (Implemented in the viewDidLoad() method):

DateSubviewController *dateSubviewController = [[DateSubviewController alloc] init];
//This is the tableViewController which handles the code from the tableView I want to implement. 

NSArray *views = [NSArray arrayWithObjects: [dateSubviewController view], nil];

self.scrollView.delegate = self;
self.pageControlBeingUsed = YES;

for (int i = 0; i < views.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UITableView *subview = [views objectAtIndex:i];
    [self.scrollView addSubview:subview];

    subview = nil;
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * views.count, self.scrollView.frame.size.height);

The scrollview is working on it's own (Tested it with different colored frames).

The problem seems to be, that [dateSubviewController view] doesn't call the methods required for the tableView to Work. The view outlet is hooked up with the tableView but "greyed out" in the storyboards. I thought it might be pointing to a wrong view, so I already tried deleting the tableView and hook it up again. This had no effect. The crash appears right after the whole viewDidLoad() method is done.

When I try to use

NSArray *views = [NSArray arrayWithObjects: [dateSubviewController dateTable], nil];

(dateTable is the tableView I try to implement) to access the tableView directly the array views contains 0 elements in debugging. The TableView delegate methods aren't called either. But it doesn't crash.

I don't know what to do anymore. I'm working on this problem for about 6 hours now, debugged several times, did Internet research but didn't find anything. Thanks for your time!

回答1:

After hours of work I now figured out this was a problem with the autoreleasepool. When I initialised the viewController...

DateSubviewController *dateSubviewController = [[DateSubviewController alloc] init];

...it was a local variable and got released as soon as the viewDidLoad() method finished. I had to use it as an instancevariable! I hope this fixes the error for a future reader too!

In headerfile of the class where you want to initialise the ViewController:

@property (strong, nonatomic) DateSubviewController * dateSubviewController;

Then initialise it in this way:

    self.dateSubviewController = [[DateSubviewController alloc] init];


回答2:

According to the error, it should be clear to you that your tableView's dataSource does not respond to the method the tableView needs. Since I guess that you already have an object that should be this dataSource (one that implements tableView:numberOfRowsInSection:), it's likely that you did not set the tableView's dataSource property to this object, but to another one. Check your code or your Xibs, and fix your tableView's dataSource link.

Do this first.

You may have other problems after. You'll see. But fix this first, and learn to read and understand your errors. Document yourself on words that you do not understand, such as "instance", "selector", "exception". Understanding errors is the key to successful debugging.