简体   繁体   English

在Swift中将UITableView委托和dataSource分配给super

[英]Assigning UITableView delegate and dataSource to super in Swift

I am trying to have a super class fill and handle the contents of a table for a certain segment. 我正在尝试填充一个超类并处理某个段的表的内容。 So I thought of implementing: 所以我想到了实现:

@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()
}

Yet I have an error. 但是我有一个错误。 By way of testing, the compiler suggested me to use: 通过测试,编译器建议我使用:

    @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()
}

whatever the meaning of construct super.self() 不管构造super.self()的含义是什么

Yet, notwithstanding the code passes through there without any problem, the command seems to be ignored and the delegate methods are called on the same class instead of the super one and even printing the value of super.self() shows it is the current class, notwitstanding, when I skip the (), Xcode encourges me by saying: 然而,尽管代码毫无问题地通过了那里,但该命令似乎被忽略了,并且委托方法是在同一个类而不是父类上调用的,甚至打印了super.self()的值也表明它是当前类。 ,不为人所知,当我跳过()时,Xcode鼓励我说:

Function produces expected type 'LogsViewController'; 函数产生预期的类型'LogsViewController'; did you mean to call it with '()'? 您是说用“()”来称呼它吗?

Yet when I add the double parenthesis it returns the current class instead of the top LogsViewController. 但是,当我添加双括号时,它将返回当前类,而不是顶部的LogsViewController。 What is the correct way of implementing what I need, and why super.self() does not work as it is advertised? 实现我所需的正确方法是什么?为什么super.self()不能像宣传的那样工作?

I clarify I cannot simply call the delegate methods on super as that shall retain the ownership to the bottom class and virtual methods on a top class shall find the methods in the bottom class instead of the one of the super one as it would be needed. 我要澄清的是,我不能简单地在super上调用委托方法,因为它将保留对底层类的所有权,而在顶层类上的虚拟方法将在底层类中找到方法,而不是需要的超级类之一。

That its the structure of my code, hoping it makes things clearer: Bottom Class 那就是我代码的结构,希望它使事情变得更清楚: Bottom 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!
}

Top "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)
    }
}

The implementations of sourceArray() and arrayOfContentsFromSearchArray() are defined both in the bottom class and its super class both inheriting from this class. sourceArray()和arrayOfContentsFromSearchArray()的实现都是在底层类及其父类中定义的,它们都继承自该类。

Simply because when you are assigning delegate you are assigning instances. 仅仅是因为在分配委托时,您是在分配实例。 So if the delegate method is called it will call the object's method which is an instance of "self" class. 因此,如果调用了委托方法,它将调用该对象的方法,该方法是“ self”类的实例。

When you assign it to super it keeps referencing to your instance and it doesn't matter if you used "self" or "super"(this is to confirm). 当您将其分配给super时,它会继续引用您的实例,并且使用“ self”还是“ super”都没有关系(这是要确认的)。

What you should do, in your class implementation of the delegate method call the super's implementation. 您应该做的是,在委托方法的类实现中调用超类的实现。

you have to put: 您必须输入:

myTableView.delegate = self
myTableView.dataSource = self

When you assign it to super it keeps referencing to your object and it doesn't matter if you used "self" or "super". 当您将其分配给super时,它将始终引用您的对象,并且使用“ self”还是“ super”都没有关系。

A solution for this case would be: 这种情况的解决方案是:

  • Superclass implements tableview's delegate and datasource methods. 超类实现tableview的委托和数据源方法。
  • A separate class (called for example TableDelegate ) which implements tableview's delegate and datasource methods. 一个单独的类(例如,称为TableDelegate ), TableDelegate实现tableview的委托和数据源方法。

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

When superclass delegate, datasource are needed to be called, assign self, Swift will find the implementation from the superclass, otherwise assign to the class which is used only as delegate and datasource for the tableview. 当需要调用超类委托,数据源,分配自身时,Swift将从超类中找到实现,否则分配给仅用作表视图的委托和数据源的类。

I ended up, as also suggested, by calling the super on all delegate methods. 正如建议的那样,我最后在所有委托方法上调用了super。 Of course it would have been cleaner to switch the table or its delegate and dataSource to the top class, but that simply does not work in Swift. 当然,将表或其委托和dataSource切换到顶级类会更干净一些,但是在Swift中根本不起作用。

Yet, even this pushes the virtual function to call the bottom implementation. 然而,即使如此,也迫使虚函数调用了底层实现。 Calling the top delegate methods does not apparently change the active instance the virtual function finds when implementing its methods. 调用顶级委托方法显然不会改变虚拟函数在实现其方法时发现的活动实例。 Of course I would need to awkwardly send upwards even the implementations, but this is definitely a bug of the support. 当然,我甚至需要笨拙地向上发送实现,但这绝对是支持的一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM