2016-03-22 8 views
0

テーブルビュー呼び出しで特定のセルを返すロジックを実装しています。 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を返す - できません。この関数はオプションを返しません。

コメントとフィードバックに感謝します。

答えて

2

このシナリオでアプリケーションを終了するには、assertionFailure(),preconditionFailure()またはfatalError("Unexpected section in TableView")のような着信機能を呼び出すことができます。

+0

ありがとうございます! fatalError( "TableViewの予期しないセクション\\(indexPath.section)")です。 –

0

私はあなたと同じことをしています。何も表示されないかもしれませんが、少なくともアプリケーションはユーザにとって重要なクラッシュしません。

関連する問題