改变故事板时获得nil

时间:2017-11-06 13:51:45

标签: ios swift

我正在iOS上开发迷你电子商务,我发现我的代码存在问题。

我在第一个屏幕上显示产品,当用户点击某个项目时,它会在另一个屏幕上显示他的数据。

我收到此错误:致命错误:在解包可选值时意外发现nil

我想我的代码会在屏幕加载之前尝试为插座设置值。

我的代码:

FeedOculosTableView.swift

android.app.Fragment;

FeedOculosDetalhesViewController.swift

class FeedOculosTableView: UIViewController, UITableViewDataSource, UITableViewDelegate {

let cellId = "cellId"

var oculosFeed: OculosFeed?

override func viewDidLoad() {
    super.viewDidLoad()
    oculosFeed = OculosFeed()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segueDetalhes" {
        if let oculosDetalhe = segue.destination as? FeedOculosDetalhesViewController {
            let selectedOculos = self.oculosFeed?.oculos[(sender as! IndexPath).section]?.1[(sender as! IndexPath).row]
            oculosDetalhe.oculos = selectedOculos
        }
    }
}

/*
 methods for personalize the table
 */

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

    cell.oculos = oculosFeed?.oculos[indexPath.section]?.1[indexPath.row]

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    DispatchQueue.main.async(){
        self.performSegue(withIdentifier: "segueDetalhes", sender: indexPath)
    }
}
}

0 个答案:

没有答案
相关问题