How to constraint UITableView over UIView in UIView subclass and keep dataSource and delegate methods in UIViewController?

85 views Asked by At

How to constraint UITableView over UIView in UIView subclass and keep dataSource and delegate methods in UIViewController?

2

There are 2 answers

2
Shehata Gamal On BEST ANSWER

Create a var inside the custom view like

weak var delegate:VCName?

Then inside it add the table and set

self.tableView.delegate = delegate
self.tableView.dataSource = delegate

class CustomView:UIView {

    weak var delegate:VCName?
    init(frame: CGRect,del:VCName) {
        super.init(frame: frame)
        delegate = del
        setup()
    }
    func setup() {
      // add table here with constraints 
      self.tableView.delegate = delegate
      self.tableView.dataSource = delegate
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
3
mistersister On

Make the constraints inside myView and set the delegate and datasource in VC like:

myView.myTableView.delegate = self
myView.myTableView.dataSource = self