2

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.

Saad Qureshi
  • 728
  • 2
  • 7
  • 21

2 Answers2

3

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

Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
  • I just tried that. The problem is with the `let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! RestaurantMapTableViewCell` But setting the identifier or class does not fix the problem. They were already in place. – Saad Qureshi Dec 23 '16 at 04:33
  • Try remove "as! RestaurantMapTableViewCell". If it's going well after, the problem is in cell Xib. Check the IBOutlets in cell, or make Breakpoints in cell initialization methods to find it out. – Yun CHEN Dec 23 '16 at 04:58
  • Thanks, the problem was with the nib `let nib = UINib(nibName: "RestaurantMapTableViewCell", bundle: nil)` `self.tableView.register(nib, forCellReuseIdentifier:"RestaurantMapTableViewCell") ` I removed these lines and code started working fine. – Saad Qureshi Dec 23 '16 at 07:42
  • @SaadQureshi, interesting! Did you put Cell in TableView in Storyboard, and register another XIB file? – Yun CHEN Dec 23 '16 at 08:24
  • 1
    I just had the same issue. It was caused by a broken outlet to the code of my custom cell class. I had created an outlet from a label to the custom cell class, changed my mind and deleted the outlet in my code, and I forgot to delete it from the storyboard. The crash went away when I did so. – ClayJ Mar 03 '18 at 01:25
1

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.

Medhi
  • 2,656
  • 23
  • 16