SWIFT, as adding a tableview from second class

476 Views Asked by At
class FirstClass: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        SecondClass()

    }
}
class TableView: FirstClass {
    var bodyTableView1: UITableView!

    override init() {
        super.init(nibName: nil, bundle: nil)

        bodyTableView1 = UITableView(frame: CGRectMake(0, 0, 250, 250 ))
        bodyTableView1.backgroundColor = UIColor.whiteColor()

        self.view.addSubview(bodyTableView1)
    }
}

I've tried many ways but managed not to add anything from the second class.

If you could give me an example of the most basic is the appreciate.

Thank you!

1

There are 1 best solutions below

0
On

Pls Modify Your TableView Like Below:

class TableView: UIView,UITableViewDataSource,UITableViewDelegate
{

    var vc:UIViewController!

    var bodyTableView1: UITableView!

    func addActionBar(vc:UIViewController)
    {
        self.vc=vc;
        bodyTableView1 = UITableView(frame: CGRectMake(0, 0, 250, 250 ))
        bodyTableView1.backgroundColor = UIColor.whiteColor()
        bodyTableView1.dataSource = self
        bodyTableView1.delegate = self
        vc.view.addSubview(bodyTableView1)
    }
//Pls add tableview delegate methods here
}

And also add below code in FirstClass:

class FirstClass: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        var ab : TableView = TableView(frame: CGRectMake(0, 0, 250, 250))
        self.view.addSubview(ab)
        ab.addActionBar(self)

    }
}