我有一个包含 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
}
}
Storyboard中的所有连接都已正确配置。
当我运行这段代码时,出现以下错误:
但是,当我删除 UITableViewDataSource 协议(protocol)时,异常消失了。
请让我知道如何修复异常。
编辑:
我刚刚发现异常是:
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! RestaurantMapTableViewCell
声明。
但我不知道如何修复它,我已经尝试设置标识符并为单元分配类。
最佳答案
你的代码看起来不错,所以我提供了一种方法来帮助发现问题。
首先,在句子上打个断点:
return UserBusinesses.returnedBusinesses.count
检查 returnedBusinesses 是否为 nil。如果此检查通过,则继续第二次检查,在句子上打断点:
let cell = tableView.dequeueReusableCell(...
正常,步骤会帮你找到问题所在。
如果问题出在语句“let cell = tableView.dequeueReusableCell”,你可能忘记设置单元格的标识符:
关于ios - UITableViewDataSource 导致 libc++abi.dylib : terminating with uncaught exception of type NSException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41282747/