我已经对 UITableViewController
进行了子类化,我正在通过 segue 实例化它。
似乎 TableView Controller 已正确实例化,因为正在调用 viewDidLoad
和 numberOfSectionsInTableView
。
事实上,numberOfSectionsInTableView
甚至被调用两次。第一次调用时,tableView
具有有效值,第二次调用时(第一次调用后),tableView
为 nil
。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [super numberOfSectionsInTableView:tableView] + 1 ;
}
我正在使用在 Interface Builder 中设置的静态单元格(在第 0 部分第 0 行中),并在第二部分(第 1 部分)中添加动态行数。这就是为什么我在 [super numberOfSectionsInTableView:tableView]
中添加了 1。父级的 tableView 类中没有自定义代码,一切都只是在 Interface Builder 中设置。
所有 socket 似乎都设置正确:
当代码继续执行时,应用程序崩溃并显示错误消息:
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayI objectAtIndex:]:索引 1 超出范围 [0 .. 0]
您知道什么可能导致该错误吗?
最佳答案
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [super numberOfSectionsInTableView:tableView] + 1 ;
}
基本上意味着每次调用 numberOfSectionsInTableView
时,表中的节数都会增加 1。话虽这么说,如果表中只包含两个节,那么您'最好使用 return 2;
。
现在,谈谈你的错误。考虑到我不知道确切的上下文或错误发生的位置,这很难说,但我的第一个猜测是崩溃发生在 cellForRowAtIndexPath
中,因为您的数据源要么为零,要么是一个空数组。
关于objective-c - numberOfSectionsInTableView 之后 UITableViewController 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767834/