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
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With