无法从 swift4.2 的数据源获取单元格

failed to obtain a cell from its dataSource for swift4.2

以下代码有什么问题:

// MARK: - TABLEVIEW DATA SOURCE
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, 
numberOfRowsInSection section: Int) -> Int {
return weeklyWeather.count
}

func tableView(tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
as! DailyWeatherTableViewCell

let dailyWeather = weeklyWeather[indexPath.row]
if let maxTemp = dailyWeather.maxTemperature {
  cell.temperatureLabel.text = "\(maxTemp)"
}
cell.weatherIcon.image = dailyWeather.icon
cell.dayLabel.text = dailyWeather.day

return cell
}

我尝试将 UITableViewDataSource 和 UITableViewDelegate 协议添加到 ViewController 声明。

和我的每日ViewController:

class DailyViewController: UIViewController, 
 CLLocationManagerDelegate {
 // MARK: properties
 @IBOutlet weak var weatherIcon: UIImageView!
 @IBOutlet weak var summaryLabel: UILabel!
 @IBOutlet weak var sunriseTimeLabel: UILabel!
 @IBOutlet weak var sunsetTimeLabel: UILabel!
 @IBOutlet weak var lowTemperatureLabel: UILabel!
 @IBOutlet weak var highTemperatureLabel: UILabel!
 @IBOutlet weak var precipitationLabel: UILabel!
 @IBOutlet weak var humidityLabel: UILabel!

和周表ViewController:

class WeeklyTableViewController: UITableViewController {

// MARK: - PROPERTIES
@IBOutlet weak var currentTemperatureLabel: UILabel?
@IBOutlet weak var currentWeatherIcon: UIImageView?
@IBOutlet weak var currentPrecipitationLabel: UILabel?
@IBOutlet weak var currentTemperatureRangeLabel: UILabel?

但我无法从其数据源获取单元格

整个异常是:

Terminating app due to uncaught exception 
'NSInternalInconsistencyException', reason: 'UITableView 
(<UITableView: 0x7fcfa7038600; frame = (0 0; 414 896); 
clipsToBounds = YES; autoresize = W+H; gestureRecognizers = 
<NSArray: 0x600003a31800>; layer = <CALayer: 0x600003476ca0>; 
 contentOffset: {0, -88}; contentSize: {414, 684}; 
 adjustedContentInset: {88, 0, 34, 0}>) failed to obtain a cell 
 from its dataSource (<Rainy.WeeklyTableViewController: 
 0x7fcfa800c200>)'

你可以打印 weeklyWeather.count 来查看它的编号吗?

您还可以通过在 tableView: cellForRowAt 方法的开头设置断点来查看单元格。

希望对您有所帮助。