导致 libc++abi.dylib 的 UITableViewDataSource:以 NSException 类型的未捕获异常终止
UITableViewDataSource causing libc++abi.dylib: terminating with uncaught exception of type NSException
我有一个 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
声明。
但我不知道如何解决,我试过设置标识符并将 class 分配给单元格。
你的代码看起来不错,所以我提供了一种方法来帮助发现问题。
首先,在句子上打断点:
return UserBusinesses.returnedBusinesses.count
检查 returnedBusinesses 是否为 nil。如果这次检查通过,继续第二次检查,在句子上打断点:
let cell = tableView.dequeueReusableCell(...
使用这个按钮一步步执行,直到崩溃的那句话:
正常,步骤会帮你找到问题所在。
如果问题出在句子 "let cell = tableView.dequeueReusableCell" 上,您可能忘记设置单元格的标识符:
或者忘记设置自定义 class:
重新检查云辰上面指出的内容,如果再次失败,请验证heightforrow方法。如果您在非自动布局单元格上使用 automaticdimension,它会在加载时崩溃。
我有一个 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
声明。
但我不知道如何解决,我试过设置标识符并将 class 分配给单元格。
你的代码看起来不错,所以我提供了一种方法来帮助发现问题。
首先,在句子上打断点:
return UserBusinesses.returnedBusinesses.count
检查 returnedBusinesses 是否为 nil。如果这次检查通过,继续第二次检查,在句子上打断点:
let cell = tableView.dequeueReusableCell(...
使用这个按钮一步步执行,直到崩溃的那句话:
正常,步骤会帮你找到问题所在。
如果问题出在句子 "let cell = tableView.dequeueReusableCell" 上,您可能忘记设置单元格的标识符:
或者忘记设置自定义 class:
重新检查云辰上面指出的内容,如果再次失败,请验证heightforrow方法。如果您在非自动布局单元格上使用 automaticdimension,它会在加载时崩溃。