0

Background

I am creating an extension for UITableViewController like so:

func attachToController<C: UIViewController>(_ sender: C, alignTo: UIView, to: ALEdge = .top , withOffset: CGFloat = 0.0)
    where C: UITableViewDataSource, C:UITableViewDelegate
{
    self.estimatedRowHeight = 44
    ..
    self.delegate = sender as UITableViewDelegate
    self.dataSource = sender as UITableViewDataSource
}

Currently the class that's using this extension is declared like so:

final class MyViewController: UIViewController {

then I call this in view did load:

override func viewDidLoad() {
    super.viewDidLoad()
    self.setupTableView()
}

..

func setupTableView() {
    self.tableView.attachToController(self, alignTo: self.view, withOffset: 0.0)
    ..
 }

But I'm getting the error

Ambiguous reference to member 'tableView'

I would like to declare MyViewController so that it extends UIViewController and also conforms to the UITableViewDataSource and UITableViewDelegate protocols.. ideas?

Note: I'm using Swift 4.2.

halfer
  • 19,824
  • 17
  • 99
  • 186
abbood
  • 23,101
  • 16
  • 132
  • 246

1 Answers1

1

Just add it

final class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

Also if the attachToController(...) extension is on UITableViewController it won't work, I assume according to how you call it, you want it to be on UITableView

olejnjak
  • 1,163
  • 1
  • 9
  • 23