Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the tableview nil?

I have a ViewController and inside of that I have a TableView. After I get the data with an Alamofire Request I reload the data of the TableView and there is where it crashes because the tableView is nil.

class TeamsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet var tableView: UITableView!

var terminArr = [Termin]()
override func viewDidLoad() {        
    TerminResource.getTerminsByUsername(username: "SonnyBlackzz"){
        response in

        self.terminArr = response
        self.tableView.reloadData()
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.terminArr.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    print(self.terminArr[indexPath.row].name)

    cell.textLabel?.text = self.terminArr[indexPath.row].name

    return cell
}
like image 770
Adrian I. Avatar asked Oct 18 '22 15:10

Adrian I.


1 Answers

You will have three options maybe help you .

1-Make sure that you have connected the outlet to your table view .

2-you can change your inherit to be as below

class TeamsVC: UITableViewController, UITableViewDelegate, UITableViewDataSource{
.
.
.
}

the you will have easily access

self.tableView.reloadData()

3- You may have to init it programatically

first

var tableView: UITableView  =   UITableView()

then in viewdidload

tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
tableView.delegate      =   self
tableView.dataSource    =   self
like image 154
Mina Nabil Avatar answered Oct 21 '22 03:10

Mina Nabil