处理不会在 tableView(_:cellForRowAtIndexPath) -> UITableViewCell 中发生的情况

Handling cases that won't occur in tableView(_:cellForRowAtIndexPath) -> UITableViewCell

我正在为 table 视图调用中的特定单元格实施逻辑 return。如果我使用 switch 语句,有什么建议可以覆盖 "default" 案例?

参见此处示例:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    switch indexPath.section {
    case 0: // CompanyDetailsGeneralTableViewCell
        let cell = tableView.dequeueReusableCellWithIdentifier("CompanyDetailsGeneral", forIndexPath: indexPath) as! CompanyDetailsGeneralTableViewCell

        // STUFF FOR CompanyDetailsGeneralTableViewCell

        return cell
    case 1:
        let cell = tableView.dequeueReusableCellWithIdentifier("CompanyResources", forIndexPath: indexPath) as! CompanyResourcesTableViewCell

        return cell
    default:
        // Can't reach here (never!) as table has only 2 sections.
        return UITableViewCell() // Hack. BEST PRACTICE?
    }
}

在我考虑过的方法中:

希望收到评论和反馈。

您应该能够调用 assertionFailure()preconditionFailure()fatalError("Unexpected section in TableView") 等终止函数,以便在该情况下终止应用程序。

我做的和你做的一样。它可能不会显示任何内容,但至少应用不会崩溃,这对用户来说很重要。