cellForRowAt: returns nil when changing tabs

Viewed 45

I am pretty new to Swift and I have a problem that I've been struggling with.

I have a ViewController that has a tableview and this ViewController in embedded in a TabViewController .Every time I change tabs the app crashes and I guess it's from the method cellForRowAt: and the fact that it returns nil.

I get this errors:

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value:

And it points here : tableView.delegate = self

This is the code :

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
        self.tableView.reloadData()

    }

I reload data after 0.3 because in viewDidLoad I get a JSON from server and it takes time to process all the data

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "AlertsNotSolved") as! AlertsNotSolved
    cell.isUserInteractionEnabled = false
    cell.dateLabel.text = dateArray[indexPath.item]
    cell.notValidLabel.text = stateArray[indexPath.item]
    cell.problemNameLabel.text = nameArray[indexPath.item]
    return cell

}

dateArray,stateArray and nameArray are arrays that I fill with data from JSON and have the same number of elements.

Any suggestions?

0 Answers
Related