在 Swift 中将 UITableView 委托和数据源分配给 super

Assigning UITableView delegate and dataSource to super in Swift

我正在尝试让超级 class 填充和处理某个段的 table 的内容。所以我想到了实施:

@IBAction func changeValue(sender:AnyObject){
    self.searchDisplayController?.setActive(false, animated:false)
    if (selection.selectedSegmentIndex==1){
        self.myTableView.delegate=super
        self.myTableView.dataSource=super

    } else {
        self.myTableView.delegate=self
        self.myTableView.dataSource=self
    }
    self.myTableView.reloadData()
}

但是我有一个错误。通过测试,编译器建议我使用:

    @IBAction func changeValue(sender:AnyObject){
    self.searchDisplayController?.setActive(false, animated:false)
    if (selection.selectedSegmentIndex==1){
        self.myTableView.delegate=super.self()
        self.myTableView.dataSource=super.self()

    } else {
        self.myTableView.delegate=self
        self.myTableView.dataSource=self
    }
    self.myTableView.reloadData()
}

无论构造的含义如何 super.self()

然而,尽管代码通过那里没有任何问题,命令似乎被忽略并且委托方法在同一个 class 而不是超级方法上被调用,甚至打印 [= 的值47=]() 显示它是当前的 class,尽管如此,当我跳过 () 时,Xcode 鼓励我说:

函数产生预期类型'LogsViewController';您是想用“()”调用它吗?

然而,当我添加双括号时,它 returns 当前 class 而不是顶部的 LogsViewController。 实现我需要的正确方法是什么?为什么 super.self() 不能像宣传的那样工作?

我澄清一下,我不能简单地调用 super 上的委托方法,因为它会保留底部 class 的所有权,顶部 class 上的虚拟方法会找到底部的方法 class 而不是所需的超级之一。

这是我的代码结构,希望它能让事情更清楚: 底部Class

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    let isMainTable = tableView==self.myTableView
    let isFavorite = selection.selectedSegmentIndex==2
    var count: Int?
    count = sourceForTableKind(isMainTable, favorites:isFavorite)?.count
    if (count==nil) {
        return 0
    }
    return count!
}

置顶"virtual class":

func sourceArray()->Array<NearElement>?{
    return nil
}

func arrayOfContentsFromSearchArray(searchArray:Array<String>?, favorites:Bool)->Array<NearElement>?{
    return nil
}

func sourceForTableKind(normal: Bool, favorites:Bool)->Array<NearElement>?{
    // NSLog(@"source:%@ %@",favorites?@"favorites": @"near", normal?@"normal":@"searched");
    print("sono \(self)")
    if (normal) {
        return sourceArray()
    } else {
        return arrayOfContentsFromSearchArray(searchResults, favorites:favorites)
    }
}

sourceArray() 和 arrayOfContentsFromSearchArray() 的实现都定义在底部 class 和它的超 class 都继承自这个 class.

很简单,因为当您分配委托时,您就是在分配实例。 因此,如果调用委托方法,它将调用对象的方法,该方法是 "self" class.

的实例

当您将它分配给 super 时,它会一直引用您的实例,无论您使用的是 "self" 还是 "super"(这是为了确认)。

你应该做什么,在你的 class 委托方法的实现中调用 super 的实现。

你必须输入:

myTableView.delegate = self
myTableView.dataSource = self

当您将它分配给 super 时,它会一直引用您的对象,无论您使用的是 "self" 还是 "super"。

这种情况的解决方案是:

  • Superclass 实现了 tableview 的委托和数据源方法。
  • 一个单独的 class(例如 TableDelegate)实现了 tableview 的委托和数据源方法。

       if (selection.selectedSegmentIndex == 1) {
         self.myTableView.delegate = self;
         self.myTableView.dataSource = self;
        } else {
            self.myTableView.delegate = instanceOfTableDelegate;
            self.myTableView.dataSource = instanceOfTableDelegate;
        }`
    

当superclass需要调用delegate、datasource时,赋值self,Swift会从superclass中寻找实现,否则赋值给class 仅用作表视图的委托和数据源。

最后,正如所建议的那样,我在所有委托方法上调用了 super。当然,将 table 或其委托和数据源切换到顶部 class 会更干净,但这在 Swift.

中根本行不通

然而,即使这样也会推动虚函数调用底层实现。调用顶级委托方法显然不会更改虚拟函数在实现其方法时找到的活动实例。当然,我什至需要笨拙地向上发送实现,但这绝对是支持的一个错误。