У меня есть ViewController, содержащий 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
}
}
Все соединения в раскадровке настроены правильно.
Когда я запускаю этот код, он дает следующую ошибку:
Однако, когда я удаляю протокол UITableViewDataSource, исключение исчезает.
Пожалуйста, дайте мне знать, как исправить исключение.
РЕДАКТИРОВАТЬ: я только что узнал, что исключение связано с оператором let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! RestaurantMapTableViewCell
.
Но я не знаю, как это исправить, я попытался установить идентификатор и присвоить класс ячейке.
//as! RestaurantMapTableViewCell
прокомментировал? - person Bista   schedule 22.12.2016UserBusinesses
или массивreturnedBusinesses
равен нулю. - person Lefteris   schedule 22.12.2016