问题描述
我正在尝试设置UITableViewController,以使其tableView属于我的自定义子类.我的loadView方法当前看起来像这样:
I am trying to setup a UITableViewController so that its tableView belongs to my custom subclass. My loadView method currently looks like this:
- (void) loadView {
MyTableViewSubclass* tv = [[[MyTableViewSubclass alloc]initWithFrame: CGRectZero style: UITableViewStylePlain]autorelease];
self.view = tv;
self.tableView = tv;
}
如果我注释掉上述方法,稍后我会崩溃.因此,缺少了一些东西.但是呢?
I am getting crashes later on that go away if I comment out the above method. So something is missing. But what?
Apple的文档说我不应该在loadView中调用super .这是有道理的,因为我希望视图具有我的课程,而不是他们的课程.
Apple's documentation says I should not be calling super in loadView. Which makes sense, because I want the view to have my class, not theirs.
我尝试过的事情无济于事:
Things I've tried that don't help:
- 重写loadView方法,以便它创建一个普通的UITableView.这告诉我问题的根源不在于子类的实现.
- 从我的loadView方法调用[super viewDidLoad].从Apple的文档来看,我不清楚该方法是从loadView调用还是在之后调用.无论如何,将其添加到我的loadView方法的末尾都无济于事.
我尝试过的一件事确实解决了问题,但未能达到目的:
One thing I've tried that does fix the problem, but defeats the purpose:
- 注释掉我的loadView方法.
崩溃显示如下.在用户进行一些输入之后发生.如果我创建一个普通的UITableView而不是我的子类,它也会以相同的方式发生.应用程序中发生了很多事情,而我的loadView覆盖中的某些内容[或更可能是我的覆盖中的某些内容丢失]导致状态有所不同,进而导致崩溃.但我看不出有什么好的方法来追踪不同之处.
the crash is shown below. It happens after the user does some input. It also happens the same way if I am creating a plain UITableView instead of my subclass. There is a lot going on in the app, and something in my loadView override [or more likely, something missing from my override] is causing the state to be different, which in turn leads to the crash. But I don't see a good way to track what is different.
2011-09-08 12:44:59.591 MyAppName[97649:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[MyTableViewSubclass scrollToRowAtIndexPath:atScrollPosition:animated:]: row (0) beyond bounds (0) for section (0).'
推荐答案
原来,在加载过程中,我需要设置dataSource和tableView的委托.因此,当我这样做时,一切正常:
Turns out I need to set the dataSource and delegate of my tableView as a part of loading. So when I do this, everything works fine:
- (void) loadView {
MyTableViewSubclass* tv = [[[MyTableViewSubclass alloc]initWithFrame: CGRectZero style: UITableViewStylePlain]autorelease];
tv.dataSource = self;
tv.delegate = self;
self.view = tv;
self.tableView = tv;
}
这篇关于覆盖UITableViewController子类中的loadView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!