objective-c - numberOfSectionsInTableView 之后 UITableViewController 崩溃

标签 objective-c ios uitableview uiviewcontroller uikit

我已经对 UITableViewController 进行了子类化,我正在通过 segue 实例化它。

似乎 TableView Controller 已正确实例化,因为正在调用 viewDidLoadnumberOfSectionsInTableView 。 事实上,numberOfSectionsInTableView 甚至被调用两次。第一次调用时,tableView 具有有效值,第二次调用时(第一次调用后),tableViewnil

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [super numberOfSectionsInTableView:tableView] + 1 ;
}

我正在使用在 Interface Builder 中设置的静态单元格(在第 0 部分第 0 行中),并在第二部分(第 1 部分)中添加动态行数。这就是为什么我在 [super numberOfSectionsInTableView:tableView] 中添加了 1。父级的 tableView 类中没有自定义代码,一切都只是在 Interface Builder 中设置。

所有 socket 似乎都设置正确:

enter image description here

当代码继续执行时,应用程序崩溃并显示错误消息:

由于未捕获的异常“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/

相关文章:

ios - 在后台播放的音频在 iOS 模拟器中不起作用

ios - XCode 错误 itms-90035 - 签名无效?

ios - 为什么我的 NO RESULTS 单元格没有显示在我的 TableView Controller 中?

ios - 将 NSFetchedResultsController 与 UITableView 中的特定部分相关联

iphone - initWithCoder 没有设置 UILabel 属性(UIFont 和文本颜色)

ios - 如何更改ios中特定单词的颜色

ios - 如果设备 token 发生变化,如何向多个设备(属于一个用户)发送推送通知?

ios - 无法在 UIScrollView 中选择/编辑 UITextView 中的文本

iphone - 按钮图像未显示,但其他图像已在其位置

swift - 代码 : How to find which cell has no reuse identifier