2017-07-27 3 views
1

私は2つのセクションでTableViewを構築しようとしています。 2番目のセクションは配列のデータで埋められる必要があります。 Storyboardには静的セルを持つTableViewがあります。それぞれに1つのセルを持つ2つのセクションがあります。Swift Static Table View追加の行を追加する

キャッチされない例外により「NSRangeException」、理由にもアプリを終了

*でより多くの項目がある場合のみそれだけで正常に動作textSectionTwo配列内のアイテムが、休憩がある場合:「* - [__ NSSingleObjectArrayI objectAtIndex:]:境界を超えてインデックス1 [0 .. 0]」コードの下

enum TableViewSections { 
    case sectionOne 
    case sectionTwo 
} 

class TableViewController: UITableViewController { 

    struct Identifier { 
     static let sectionOneCell = "SectionOne" 
     static let sectionTwoCell = "SectionTwo" 
    } 

    let textSectionOne = "Section 1" 
    let textSectionTwo = ["Section 2 - Row 1", "Section 2 - Row 2"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.register(UITableViewCell.self, forCellReuseIdentifier: Identifier.sectionOneCell) 
     tableView.register(UITableViewCell.self, forCellReuseIdentifier: Identifier.sectionTwoCell) 

     tableView.reloadData() 
    } 

    // MARK: - Table view data source 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     switch sectionForIndex(section) { 

     case .sectionTwo?: 
      return textSectionTwo.count  // <- breaks when return is > 1 

     case nil: 
      fatalError("Unexpected value") 

     default: 
      return super.tableView(tableView, numberOfRowsInSection: section) 
     } 
    } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     switch sectionForIndex(indexPath.section) { 
     case .sectionOne?: 
      return self.tableView(tableView, sectionOneCellForRowAt: indexPath) 

     case .sectionTwo?: 
      return self.tableView(tableView, sectionTwoCellForRowAt: indexPath) 

     case nil: 
      fatalError("Unexpected value") 
     } 
    } 


    private func tableView(_ tableView: UITableView, sectionOneCellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: Identifier.sectionOneCell, for: indexPath) 
     cell.textLabel?.text = textSectionOne 
     return cell 
    } 


    private func tableView(_ tableView: UITableView, sectionTwoCellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: Identifier.sectionTwoCell, for: indexPath) 
     cell.textLabel?.text = textSectionTwo[indexPath.row] 
     return cell 
    } 


    func sectionForIndex(_ index: Int) -> TableViewSections? { 
     switch index { 
     case 0: 
      return .sectionOne 

     case 1: 
      return .sectionTwo 

     default: 
      return nil 
     } 
    } 

} 

EDIT:
this sample code from Apppleは私の問題と似ています。彼らは静的セルを使用しており、コードを通じてコン​​テンツを追加しています。

+1

* static *はどういう意味ですか? ;-)そして、どうして彼らはInterface Builderで設計されているので、なぜ細胞を登録しますか? – vadian

+0

私は静的な意味を知っています。私はこの[サンプルコード(Appleのサンプルコード)](https://developer.apple.com/library/content/samplecode/HomeKitCatalog/Introduction/Intro.html)に目を通し、コードを通じてデータが追加される静的なセルがあります。 – Lars

答えて

2

スタティックテーブルビューは、スタティックセルのUIとデータを含むテーブルビューを表示するために使用されます。ストーリーボードで作成したセルだけが使用されます。

解決方法:ダイナミックテーブルビューを使用します。それはあなたが好きなだけ多くの行を表示することができます。

関連する問題