2017-09-12 12 views
0

以下のコードを作成しましたが、 'Section 1'ヘッダーと要素だけが表示されます。私はすべての3つのヘッダーの下に対応する要素が表示されることを期待します、なぜこれは起こっていないのですか?tableViewのセクションが表示されない

import Foundation 
import UIKit 



class summary: UIViewController, UITableViewDelegate, UITableViewDataSource { 


    var itemsInSections: Array<Array<String>> = [["1A", "1B", "1C"], ["2A", "2B"], ["3A", "3B", "3C", "3D", "3E"]] 
    var sections: Array<String> = ["Section 1", "Section 2", "Section 3"] 



    @IBOutlet var tableView: UITableView! 



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell:TableViewCell1 = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell1 


     cell.label.text = "\(itemsInSections[indexPath.section][indexPath.row])" 
     return cell 
    } 




    override func viewDidLoad() { 
     let nib = UINib(nibName: "tblcell", bundle: nil) 
     tableView.register(nib, forCellReuseIdentifier: "cell") 

     self.tableView.dataSource = self 
     self.tableView.delegate = self 

    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return self.sections.count 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.itemsInSections[section].count 
    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return self.sections[section] 
    } 





} 
+1

'numberOfSectionsInTableView(tableView:UITableView)'も呼び出されていますか?ドキュメントでは、署名は 'optional func numberOfSections(tableView:UITableView) - > Int'ですので、正しいものを実装していないので、デフォルトでは1セクションが返されていると思います。 – Larme

+0

ちょっと@Larme、あなたは完全にそこにいます、それは呼ばれていませんでした。ご助力ありがとうございます :) – MattBlack

答えて

0

方法numberOfSectionシグネチャは次のとおりです。あなたが「didnのため、

optional func numberOfSections(in tableView: UITableView) -> Int 

(設定されていない場合署名にマッチする)、それはsh ouldはデフォルトで1セクションを返します。そのため、あなたは1つのセクションだけを見ていたのです。 あなたのバージョンが確認されていません。

0

これを置き換える: -

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return self.sections.count 
    } 

をこれに: -

func numberOfSections(in tableView: UITableView) -> Int { 
       return self.sections.count 

    } 
関連する問題