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:
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.
Your code looks well, so I provide a way to help finding problem.
First, make a Breakpoint on sentence:
To check whether returnedBusinesses is nil or not. If this check is passed, continue to Second check, make a Breakpoint on sentence:
Use this button to execute step by step, until the sentence that crashes:
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:
Or forget to set the custom class: