UITableViewDataSource causing libc++abi.dylib: terminating with uncaught exception of type NSException

4.1k views Asked by At

I have a ViewController containing a UITableView:

import UIKit
import GoogleMaps

class RestaurantMapViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var mapView: GMSMapView!
    @IBOutlet weak var tableView: UITableView!

    var cameraPosition: GMSCameraPosition!
    var zoomLevel: Double = 15

    override func viewDidLoad() {

        NotificationCenter.default.addObserver(self, selector: #selector(RestaurantMapViewController.updateEntries), name: NSNotification.Name(rawValue: "UpdateEntries"), object: nil)


        let nib = UINib(nibName: "RestaurantMapTableViewCell", bundle: nil)
        self.tableView.register(nib, forCellReuseIdentifier:"RestaurantMapTableViewCell")
        tableView.delegate = self
        tableView.dataSource = self
    }

    // MARK: Notifications

    @objc private func updateEntries() {

        DispatchQueue.main.async {
            self.tableView.reloadData()
            print("Data reloaded in Maps view")
        }
    } 

    // MARK: TableView methods

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("Initiating in Cells Mapview")

        return UserBusinesses.returnedBusinesses.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        print("Writing in Cells Mapview")

        let identifier = "RestaurantMapTableViewCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! RestaurantMapTableViewCell

        let business = UserBusinesses.returnedBusinesses[indexPath.row]

        cell.restaurantName.text = business.name
        cell.restaurantTags.text = business.tags
        cell.ratingControl.rating = Int(business.rating)


        return cell
    }

}

All the connections in the Storyboard have been configured correctly.

When I run this code, it gives the following error:

enter image description here

However, when I remove the UITableViewDataSource protocol, the exception goes away.

Please let me know how to fix the exception.

EDIT: I just found out that the exception is with : let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! RestaurantMapTableViewCell statement.

But I don't know how to fix it, I have tried setting the identifier and assigning class to the cell.

2

There are 2 answers

5
Yun CHEN On BEST ANSWER

Your code looks well, so I provide a way to help finding problem.

First, make a Breakpoint on sentence:

return UserBusinesses.returnedBusinesses.count

To check whether returnedBusinesses is nil or not. If this check is passed, continue to Second check, make a Breakpoint on sentence:

let cell = tableView.dequeueReusableCell(...

Use this button to execute step by step, until the sentence that crashes: enter image description here

Normal, the steps will help you find the problem.

If the problem is on sentence "let cell = tableView.dequeueReusableCell", you might forget to set the identifier of cell:

enter image description here

Or forget to set the custom class: enter image description here

0
Medhi On

After re-check what Yun Chen pointed out above, if it fails again, please verify the heightforrow method. If you use automaticdimension on non auto-layout cells, it will crash on load.