处理不会在 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?
}
}
在我考虑过的方法中:
- 根本没有使用开关,但它在结构上似乎更适合开关。
- 抛出运行时异常。不可能,因为运行时异常在 Swift.
中并不令人兴奋
- returning nil - 不能那样做,因为这个函数 return 不是可选的。
希望收到评论和反馈。
您应该能够调用 assertionFailure()
、preconditionFailure()
或 fatalError("Unexpected section in TableView")
等终止函数,以便在该情况下终止应用程序。
我做的和你做的一样。它可能不会显示任何内容,但至少应用不会崩溃,这对用户来说很重要。
我正在为 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?
}
}
在我考虑过的方法中:
- 根本没有使用开关,但它在结构上似乎更适合开关。
- 抛出运行时异常。不可能,因为运行时异常在 Swift. 中并不令人兴奋
- returning nil - 不能那样做,因为这个函数 return 不是可选的。
希望收到评论和反馈。
您应该能够调用 assertionFailure()
、preconditionFailure()
或 fatalError("Unexpected section in TableView")
等终止函数,以便在该情况下终止应用程序。
我做的和你做的一样。它可能不会显示任何内容,但至少应用不会崩溃,这对用户来说很重要。