带有 iboutlet tableview 的子类 Swift

Subclass with iboutlet tableview Swift

我在情节提要中有一个视图控制器,带有这样的表视图:

class 是这个(我link编辑了名为 'tabella' 的 iboutlet tableview):

class RisultatiRicerca: UIViewController , UITableViewDataSource , UITableViewDelegate{

    var codeSearch = 0

    @IBOutlet public var tabella : UITableView!


    override func viewDidLoad() {

        super.viewDidLoad()
        self.tabella.estimatedRowHeight = 50
        self.tabella.rowHeight = UITableViewAutomaticDimension
        self.tabella.delegate = self
        self.tabella.dataSource = self
        self.tabella.reloadData()

    }

    @available(iOS 2.0, *)
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cella = tableView.dequeueReusableCell(withIdentifier: "risu") as! CellaRisultato

        return cella

    }

    @available(iOS 2.0, *)
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        return 10
    }

}

我的问题是:

我必须在上面创建一个子class,例如:

class TuttiRicerca: RisultatiRicerca {

    override func viewDidLoad() {

        super.viewDidLoad()
        self.codeSearch = 1
    }
}

但是当我显示 TuttiRicerca 时出现此错误('tabella' 为零):

这就像 subclass 没有 link 情节提要中的表格视图。 你能帮帮我吗?

如果你想要 IBOutlet 个引用,你不能只引用 TuttiRicera()。它如何知道您要使用故事板中的哪个场景(即使您只有一个场景)?

你需要给故事板场景一个"storyboard id",然后通过故事板实例化它(例如storyboard.instantiateViewController(withIdentifier: "..."),而不是TuttiRicera()。参见instantiateViewController(withIdentifier:)