テーブルビュー呼び出しで特定のセルを返すロジックを実装しています。 switch文を使用している場合、「デフォルト」のケースをカバーするための提案はありますか? テーブルビュー(_:cellForRowAtIndexPath)で発生しないケースを処理する - > UITableViewCell
- は全くスイッチを使用していないが、それはスイッチのため、より構造的にフィッティングようだ:私は考えられてきたアプローチの中で
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ではエキサイティングではないため不可能です。
- nilを返す - できません。この関数はオプションを返しません。
コメントとフィードバックに感謝します。
ありがとうございます! fatalError( "TableViewの予期しないセクション\\(indexPath.section)")です。 –