【发布时间】:2016-12-22 12:01:41
【问题描述】:
我有一个包含 UITableView 的 ViewController:
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发表评论? -
那是错误的。我已经编辑过了。
-
UserBusinesses类或returnedBusinesses数组为零。 -
它们不是零。它们已在上一个活动中实例化
标签: ios swift uitableview nsexception