本文介绍了从不同的 ControllerClass 实现 UITableView 作为子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个 TableView 从不同的 (Table)ViewController 实现到 Scrollview.我现在实际上已经尝试了几个小时,但我无法弄清楚问题所在.我得到的错误是:

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:]:无法识别的选择器发送到实例 0x687fdb0 2012-05-0716:47:18.966 Test2[12212:f803] * 由于未捕获而终止应用异常 'NSInvalidArgumentException',原因:'-[UISectionRowDatanumberOfSectionsInTableView:]: 无法识别的选择器发送到实例0x687fdb0'

这是我正在使用的代码段(在 viewDidLoad() 方法中实现):

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).

问题似乎是,[dateSubviewController 视图] 没有调用使 tableView 工作所需的方法.view 插座与 tableView 相连,但在故事板中变灰".我认为它可能指向错误的视图,所以我已经尝试删除 tableView 并重新连接它.这没有效果.整个 viewDidLoad() 方法完成后立即出现崩溃.

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.

当我尝试使用

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

(dateTable 是我尝试实现的 tableView)直接访问 tableView 数组 views 在调试中包含 0 个元素.也不会调用 TableView 委托方法.但它不会崩溃.

(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.

我不知道该怎么办了.我现在在这个问题上工作了大约 6 个小时,调试了几次,做了互联网研究,但没有找到任何东西.感谢您的时间!

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!

推荐答案

经过几个小时的工作,我现在发现这是 autoreleasepool 的问题.当我初始化 viewController...

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];

...它是一个局部变量,并在 viewDidLoad() 方法完成后立即释放.我不得不将它用作实例变量!我希望这也能为未来的读者修复错误!

...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!

在要初始化 ViewController 的类的头文件中:

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

@property (strong, nonatomic) DateSubviewController * dateSubviewController;

然后这样初始化:

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

这篇关于从不同的 ControllerClass 实现 UITableView 作为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 13:08