无法从swift4.2的dataSource获取单元格

时间:2019-03-24 19:16:23

标签: ios swift uitableview 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声明中。

和我的DailyViewController:

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!

和WeeklyTableViewController:

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?

但是我无法从其dataSource获取单元格

整个例外是:

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>)'

1 个答案:

答案 0 :(得分:1)

您可以打印weeklyWeather.count以查看其编号吗?

您还可以通过在tableView的开始处设置一个断点来窥视单元格:cellForRowAt方法。

希望有帮助。