简体   繁体   English

如何在 UIView 子类中通过 UIView 约束 UITableView 并在 UIViewController 中保留数据源和委托方法?

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

如何在 UIView 子类中通过 UIView 约束 UITableView 并在 UIViewController 中保留数据源和委托方法?

Create a var inside the custom view like在自定义视图中创建一个 var,如

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")
    }
}

Make the constraints inside myView and set the delegate and datasource in VC like:在 myView 中创建约束并在 VC 中设置委托和数据源,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM