代码之家  ›  专栏  ›  技术社区  ›  Shivam Gaur

我们需要在CellForRowat上做崩溃应用程序吗

  •  1
  • Shivam Gaur  · 技术社区  · 8 年前

    我对牢房内部有疑问。 如果我找不到dequeueusablecell,那么如果特定类型的单元格没有dequed,我应该显式地崩溃应用程序。

    示例-:

    guard let productImageSideViewsCell = tableView.dequeueReusableCell(withIdentifier: ProductsSideViewsCell.identifier, for: indexPath) as? ProductsSideViewsCell else {
        fatalError("productImageSideViewsCell not found at indexpath \(indexPath)")
    }
    

    这样做最好还是有其他方法

    1 回复  |  直到 8 年前
        1
  •  5
  •   vadian    8 年前

    最好的方法是强制解开排队的牢房(不是开玩笑)。

    let productImageSideViewsCell = tableView.dequeueReusableCell(withIdentifier: ProductsSideViewsCell.identifier, for: indexPath) as! ProductsSideViewsCell
    

    首先是API dequeueReusableCell(withIdentifier:for:) 始终返回有效的单元格。

    这是一种罕见的情况,强制拆解非常有用,因为它发现 设计 错误。
    如果在运行时不会改变代码的接口生成器中所有内容都连接正确 不能 撞车。

    推荐文章