我想在我的应用程序中使用表格视图。但是当我将我的视图控制器确认为UITableViewDataSource时,它会出现图片中显示的错误。我们如何在我们的类中使用UITableViewDataSource协议。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// stuff
}
我建议使用类扩展来符合协议,这种隔离使得你的代码在类成长后更易于维护,例如:
class MainViewController: UIViewController {
// ...
}
你可以使用协议来扩展它
extension MainViewController: UITableViewDataSource {
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
return nil
}
// ...
}
extension MainViewController: UITableViewDelegate {
// ...
}