首页 > 解决方案 > 尝试找到的每个建议后,IBOutlet 仍然为零

问题描述

我已经阅读了关于 SO 的“所有”步骤,并且无法修复我在 TableViewCell.xib 中的 IBOutlets 仍然为零并且从未调用过 awakeFromNib() 的错误。

错误


productTitle.text = item?.product.description

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
 //item?.product.description is not nil

我试过的:

我的手机:

class CartProductCell: UITableViewCell {

    @IBOutlet weak var productTitle: UILabel!

    override func awakeFromNib() {
        print("awaken")
    }

    var item: orderItem? {
        didSet {

            productTitle.text = item?.product.description
        }
    }
}

出队:

class CartViewController: UITableViewController {


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = sections[indexPath.section][indexPath.row]   
        let cell = tableView.dequeueReusableCell(withIdentifier: "CartProductCell", for: indexPath ) as! CartProductCell
        cell.item = item
        return cell

    }
}

实例化 ViewController

extension UIViewController {
    @objc func viewCart() {
        if Cart.sharedInstance.order.items.count == 0 {
            if let cartVC = storyboard?.instantiateViewController(withIdentifier: "EmptyCart") {
                let cartNavigationController = UINavigationController(rootViewController: cartVC)
                self.present(cartNavigationController, animated: true)
            }
        } else {
            if let cartVC = storyboard?.instantiateViewController(withIdentifier: "Cart") as? CartViewController {
                let cartNavigationController = UINavigationController(rootViewController: cartVC)
                self.present(cartNavigationController, animated: true)
            }
        }
    }
}

更新

故事板 西博 XIB 2 西布 3

有什么线索吗?

标签: iosswiftxcode

解决方案


@Larme 在评论中提供了这里的答案。

问题是我在故事板 tableViewController 和 cell.XIB 中有一个具有相同标识符的原型单元。因此它使用了故事板中的单元格,而不是我的代码所在的 XIB。

您没有使用来自 xib 的单元格的界面版本,而是来自情节提要。因此它没有出口链接


推荐阅读