ios - tableView init() 层次结构如何工作?

标签 ios swift uitableview

我正在调用不带参数的 NewsFeedTableView enter image description here
但在 NewsFeedTableView 中,我实际上用参数覆盖了 init。
enter image description here
这是如何运作的?这个被覆盖的 init 是如何被调用的?

最佳答案

您正在创建您的 NewsFeedTableView使用不带参数的初始化程序。 UIView 的指定初始化器这是这个:

init(frame: CGRect)

在任何继承自 UIView 的类的初始化期间,将始终调用指定的初始化程序。 . init() UIView 的初始化器会用 CGRect.zero 调用它作为参数。

现在,查看 init(frame:style:) 的文档UITableView 的初始化器:

If you initialize the table view with the UIView method init(frame:), the UITableView.Style.plain style is used as a default.



所以基本上:
1. NewsFeedTableView()来电NewsFeedTableView(frame: CGRect.zero)2. NewsFeedTableView(frame: CGRect.zero)来电NewsFeedTableView(frame: CGRect.zero, style: .plain)
这就是为什么您的重写初始化程序被调用的原因

关于ios - tableView init() 层次结构如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60711178/

相关文章:

ios - UITableview覆盖按钮

objective-c - 如何自定义 "Top Rated"UITabBar 项目的文本?

swift - 如何移动没有 titleBar 且完全充满 webView 的窗口?

ios - 为什么即使经过苹果验证,通用链接仍然不起作用?

swift - 使用操作表按钮传递值

swift - 便利初始值设定项 init(keyPath :) for CABasicAnimation

iphone - 编辑时 UITableView 每单元格选择模式

swift - 代码转换 swift 2 --> 3 导致索引路径处的二进制运算符错误

iphone - 应用程序的元数据被 Apple : "Cant locate In App Purchase in my App". 拒绝?

ios - 如果一个框架导入了主项目也导入的一些库怎么办?