【问题标题】:Can't override UITableViewDataSource and UITableViewDelegate无法覆盖 UITableViewDataSource 和 UITableViewDelegate
【发布时间】:2017-01-26 08:11:26
【问题描述】:
extension FormViewController : UITableViewDelegate {
    public func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?
    public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
    public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
    public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
}

extension FormViewController : UITableViewDataSource {
    public func numberOfSectionsInTableView(tableView: UITableView) -> Int
    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
    public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?
}

class NotifiableFormViewController: FormViewController

为什么我不能实现和覆盖 DataSource 和 TableViewDelegate?错误:“方法没有覆盖它的任何超类方法”

class FriendsTableViewController: NotifiableFormViewController{
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    override func tableView(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

        cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"

        return cell
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section)"
    }
}

【问题讨论】:

标签: swift uitableview


【解决方案1】:

更简单地说,删除单词override,因为您不是在覆盖该方法,而是在实现它

【讨论】:

    【解决方案2】:

    UITableViewDataSourceUITableViewDelegate 只是协议 如果你想覆盖它,你在NotifiableFormViewController中实现上述功能

    然后覆盖你的FriendsTableViewController

    但如果没有在你的超类中实现,那么你就不能覆盖它(没有覆盖实现的委托或数据源函数)

    【讨论】:

      【解决方案3】:

      根据定义,扩展只能向现有类添加功能。它不能改变,包括覆盖现有的实现。

      【讨论】:

        猜你喜欢
        • 2018-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-08
        • 1970-01-01
        • 2011-04-05
        • 1970-01-01
        相关资源
        最近更新 更多