ios – 在UITableViewController中覆盖数据源和委托方法时,是否需要调用super?

前端之家收集整理的这篇文章主要介绍了ios – 在UITableViewController中覆盖数据源和委托方法时,是否需要调用super?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
请考虑以下从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?
        ...
    }

}

documentation说:

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似乎没有任何明显的效果.

解决方法

在这些情况下无需拨打超级电话.您在原始问题中包含的文档引用是指覆盖超类方法.但是,UITableViewDataSource和UITableViewDelegate是协议(而不是您的类’超类),您提到的方法特别为您声明,以最适合您的方式实现它们.

猜你在找的iOS相关文章