1

我有一个表格栏,其中有 3 个独立的 MVC。第一个 MVC 是UITableView我可以搜索位置的地方,它将显示 10 天的预测。第二个 MVC 也UITableView存储了我最喜欢的位置。我可以点击该位置,它会给我带来另一个UITableView显示 10 天预测的位置,就像第一个标签栏一样。但是我得到一个“线程 1:致命错误:在展开可选值时意外发现 nil”我正在使用自定义原型单元格,就像第一个选项卡栏视图一样,它在那里工作得很好。在此处输入图像描述

我的第一个标签栏 mvc 的代码如下所示:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "WeatherDataCell", for: indexPath)

let weatherCell = timeSeries[indexPath.row]
if let wc = cell as? WeatherTableViewCell {
    wc.timeSeries = weatherCell
}

return cell
}

自定义原型单元看起来像:

class WeatherTableViewCell: UITableViewCell {

    @IBOutlet weak var WeatherImageView: UIImageView!
    @IBOutlet weak var WeatherTimeLabel: UILabel!
    @IBOutlet weak var WeatherTemperatureLabel: UILabel!
    @IBOutlet weak var WeatherWindLabel: UILabel!
    @IBOutlet weak var WeatherPrecipitationLabel: UILabel!

    var timeSeries : TimeSeries? {
        didSet {
            updateUI()
        }
    }

    func updateUI() {
        for parameters in timeSeries!.parameters {
        if parameters.name == "t" {
            let temperature = parameters.values[0]
            WeatherTemperatureLabel.text? = "\(temperature) °C" // <- works for the first MVC but crashes on the second MVC even though i can print out the temperature
        }
    }
}

秒 MVC 中的 func 基本上看起来与第一个相同,所以我不明白为什么它会崩溃,它应该有数据。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DetailedWeatherCell", for: indexPath) as! WeatherTableViewCell

let weatherCell = timeSeries[indexPath.row]        
let wc = cell
wc.timeSeries = weatherCell

return cell

}

为清晰起见添加了附加代码(我的 FavoriteDetailedTableViewController 类):

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(WeatherTableViewCell.self, forCellReuseIdentifier: "DetailedWeatherCell")
}
4

2 回答 2

1
  • 2 视图控制器不能重用由两个视图控制器之一创建的相同原型单元
  • WeatherTableViewCell 在 mvc1、mvc2 中不能有 2 个布局
    => 您必须在 xib 文件中创建客户单元格并注册一个单元格以供 UITableViewCell 重用。
于 2018-11-13T03:27:56.717 回答
1

我认为您忘记更改 FavoritedDetailedTableViewController 中原型单元格的标识符。在此 TableViewController 中将其更改为

在此处输入图像描述

它应该可以工作。

其次,您可能忘记在此处设置您的单元格类

在此处输入图像描述

还要从 viewDidLoad 中删除这一行

self.tableView.register(WeatherTableViewCell.self, forCellReuseIdentifier: "DetailedWeatherCell")
于 2018-11-12T21:17:32.743 回答