Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何提供支持UITableViewDataSource协议的继承视图控制器?_Ios_Swift_Inheritance_Protocols - Fatal编程技术网

Ios 如何提供支持UITableViewDataSource协议的继承视图控制器?

Ios 如何提供支持UITableViewDataSource协议的继承视图控制器?,ios,swift,inheritance,protocols,Ios,Swift,Inheritance,Protocols,我想提供一个符合UITableViewDataSource协议的帮助器视图控制器。但这个类并不是有意的,必须通过继承来使用 为此,编译器要求我在helper类中实现协议。 我怎样才能解决这个问题 class BigHeadViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // Helper class that do ton of the job } class StoryTa

我想提供一个符合UITableViewDataSource协议的帮助器视图控制器。但这个类并不是有意的,必须通过继承来使用

为此,编译器要求我在helper类中实现协议。 我怎样才能解决这个问题

class BigHeadViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // Helper class that do ton of the job
}

class StoryTableViewController: BigHeadViewController {
    // final implementation, must implement UITableViewDataSource
}

在本例中,我将有一个
UITableViewManager
,这将符合
UITableViewDelegate,UITableViewDataSource
,然后我将为每个需要不同类型数据/单元格的tableview创建子类

我会在
BigHeadViewController
上有一个指向
UITableViewManager
的指针,但实际上会将它分配给
UITableViewManager
的子类,例如
MessageTableViewManager:UITableViewManager


然后,当您子类化
BigHeadViewController
时,您可以覆盖属于
UITableViewManager
的变量,并对if进行不同的子类,以生成不同的数据。

在这种情况下,您可以在超类中实现UITableViewDataSource方法,具体如下:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    precondition(false, "UITableViewDataSource method MUST be implemented in subclass of BigHeadViewController")    

    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    precondition(false, "UITableViewDataSource method MUST be implemented in subclass of BigHeadViewController") 

    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    precondition(false, "UITableViewDataSource method MUST be implemented in subclass of BigHeadViewController") 

    return UITableViewCell()
}
在子类中实现UITableViewDataSource方法时,不要调用super。这将确保所需的方法在子类中实现,或者在控制台中为您提供错误消息(因为当没有可用的子类方法时将调用super方法)