在 UITableViewController 中覆盖数据源和委托方法时是否需要调用 super?
Do I need to call super when overriding data source and delegate methods in UITableViewController?
考虑以下从 UITableViewController
.
派生自定义视图控制器的代码片段
class Controller: UITableViewController {
...
)
// MARK: - Table View Data Source
extension Controller {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// call super?
...
}
}
// MARK: - Table View Delegate
extension Controller {
override func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {
// call super?
...
}
}
You may override loadView or any other superclass method, but if you do be sure to invoke the superclass implementation of the method, usually as the first method call.
我的问题是,这是否也适用于 UITableViewController
遵循的协议 UITableViewDataSource
和 UITableViewDelegate
中的方法?
在数据源方法中调用 super
对我来说意义不大,因为通常您使用这些方法定义自己的内容。但是,我不确定委托方法。例如,在 willBeginEditingRowAtIndexPath
中调用 super
似乎没有任何明显的效果。
你说得对,在这种情况下这样做没有意义,也不应该这样做。
在这些情况下不需要调用 super。您在原始问题中包含的文档引用是指覆盖 super class 方法。但是,UITableViewDataSource
和 UITableViewDelegate
是协议(而不是您的 class' superclass)并且您提到的方法是专门为您声明的,以便您以适合您的方式实现它们最好。
考虑以下从 UITableViewController
.
class Controller: UITableViewController {
...
)
// MARK: - Table View Data Source
extension Controller {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// call super?
...
}
}
// MARK: - Table View Delegate
extension Controller {
override func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {
// call super?
...
}
}
You may override loadView or any other superclass method, but if you do be sure to invoke the superclass implementation of the method, usually as the first method call.
我的问题是,这是否也适用于 UITableViewController
遵循的协议 UITableViewDataSource
和 UITableViewDelegate
中的方法?
在数据源方法中调用 super
对我来说意义不大,因为通常您使用这些方法定义自己的内容。但是,我不确定委托方法。例如,在 willBeginEditingRowAtIndexPath
中调用 super
似乎没有任何明显的效果。
你说得对,在这种情况下这样做没有意义,也不应该这样做。
在这些情况下不需要调用 super。您在原始问题中包含的文档引用是指覆盖 super class 方法。但是,UITableViewDataSource
和 UITableViewDelegate
是协议(而不是您的 class' superclass)并且您提到的方法是专门为您声明的,以便您以适合您的方式实现它们最好。