2017-11-01 4 views
1

TableViewsの使い方をマスターしようとしていますが、私はプログラムで入力しようとしています。これはうまくいっていますが、このプログラムを変更して、あらかじめ設定されたセクションではなく空のセクションを作成したいと思います。iOS TableViewで空のセクションを作成する

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var rowCount = 1 
    var sectionCount = 1 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return sectionCount 
    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return "Section \(section+1)" 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return rowCount 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) 
     cell.textLabel?.text = "Entry \(indexPath.row+1) in Section \(indexPath.section+1)" 
     return cell 
    } 

// func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
//   
// } 

    @IBOutlet weak var tableView: UITableView! 
    @IBOutlet weak var onSect: UISwitch! 
    @IBOutlet weak var addToTable: UIButton! 
    @IBAction func insertToTable(_ sender: Any) { 
     insert() 
    } 

    func insert(){ 
     if onSect.isOn{ 
      sectionCount += 1 
     }else{ 
      rowCount += 1 
     } 
     tableView.reloadData() 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tableView.delegate = self 
     self.tableView.dataSource = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

私はreloadData()は基本的にtableView機能に基づいてテーブルをリメイクすることを見ることができますが、私はまたreloadData()通話中に行でそれを埋めることなく、セクションを作るためにどのような方法を考えることはできません。

+0

はあなたが0行のセクションを作りたいん:ここで

はthrid 1が空であることと、すべての残りが一列のみを有する5つのセクションの例ですか? – MEnnabah

+0

基本的にはい。 –

+0

numberOfRowsInSectionで0を返さないのはなぜですか? – MEnnabah

答えて

0

throwugoutココアタッチ(とその点ではココア)のdelegation pattern & data sourceを理解する必要があります。

TableViewは、データの知識をdataSourceに委任しています。そのことについては

、データソースがあなたのケースでは、我々が最も興味を持っている、特定のメソッドを実装する必要があります:

func numberOfSections(in tableView: UITableView) -> Int 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

これら二つの機能は、あなたのテーブルビューの内容を命令します。 invoquedされる最初の関数である:あなたが返す内容に応じて

func numberOfSections(in tableView: UITableView) -> Int 

、それは第二の方法をinvoqueます、あなたはあなたのテーブルビューは、すべて一緒に空になりたい場合は、あなたは、このようにセクションならば数に0を返すことができます他の機能はインコイクされません。

numberOfSectionsの5を返すと、他の関数がインボークされ、取得したセクションインデックスの引数に応じて必要なだけ多くの行を返すことができます。

func numberOfSections(in tableView: UITableView) -> Int { 
    return 5 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 2 { 
     return 0 
    } 

    return 1 
} 
+1

この答えは、この仕組みについて私に多くのことを教えてくれました。そして、辞書ベースの回答にしました。ここでは、各セクションを追加したアイテムの数を追跡する配列をキーとして保持しました。私が探していた結果を達成しました、ありがとう。 –

+0

あなたは歓迎して、良い仕事を続けてください:) – TheFuquan

関連する問題