Joe
Joe

Reputation: 387

UITableView delegate methods not being called. Created from another class

I'm trying to add UITableView from another class but the delegate methods are not being called. Only the numberOfRowsInSection method is being called. I know I can do this in the main ViewController, but I would like to do it from another class. Is it possible? This code is fully functional, just copy and paste into a project. Thank you!

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let t = Table()
        t.create()
    }
}

class Table: UIViewController, UITableViewDelegate, UITableViewDataSource {

    private let myArray: NSArray = ["First","Second","Third"]

 func create() {
        let barHeight = UIApplication.shared.statusBarFrame.size.height
        let displayWidth = UIScreen.main.bounds.width
        let displayHeight = UIScreen.main.bounds.height

        let myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        UIApplication.shared.delegate?.window??.rootViewController?.view.addSubview(myTableView)
        myTableView.dataSource = self
        myTableView.delegate = self
        myTableView.backgroundColor = UIColor.red
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Num: \(indexPath.row)")
        print("Value: \(myArray[indexPath.row])")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("numberOfRowsInSection")
        return myArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("cellForRowAt")
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
        cell.textLabel!.text = "\(myArray[indexPath.row])"
        return cell
    }

}

Upvotes: 0

Views: 328

Answers (2)

DonMag
DonMag

Reputation: 77477

When you run this:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let t = Table()
        t.create()
    }
}

You have created a local variable t. As soon as you leave the viewDidLoad() function, t no longer exists - so there are no delegate methods to call.

Instead, create a class-level variable that will stick around:

class ViewController: UIViewController {

    let t = Table()

    override func viewDidLoad() {
        super.viewDidLoad()
        t.create()
    }
}

Upvotes: 3

KKRocks
KKRocks

Reputation: 8322

I think it is not called due to it Tablview not added to self.view or else. so you need to add this line after setting of tableview.

func create() {
        let barHeight = UIApplication.shared.statusBarFrame.size.height
        let displayWidth = UIScreen.main.bounds.width
        let displayHeight = UIScreen.main.bounds.height

        let myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        UIApplication.shared.delegate?.window??.rootViewController?.view.addSubview(myTableView)
        myTableView.dataSource = self
        myTableView.delegate = self
        myTableView.backgroundColor = UIColor.red
        self.view.addSubview(myTableView) //add this line 
    }

Upvotes: 0

Related Questions