Joey Nash
Joey Nash

Reputation: 35

iOS Create Empty Section in TableView

I'm trying to master the use of TableViews, and I'm trying to populate one programmatically. It's going well, but I'd like to modify this program to create sections empty rather than pre-populated.

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.
    }


}

I can see that reloadData() basically remakes the table based on the tableView functions, but I can't think of any way to make a section without also filling it up with the rows during the reloadData() call.

Upvotes: 2

Views: 1579

Answers (1)

TheFuquan
TheFuquan

Reputation: 1797

You have to understand the delegation pattern & data source used througout cocoa touch (and cocoa for that matter).

The TableView is delegating the knowledge of it's data to the dataSource.

For that matter, the dataSource has to implement specific methods, in your case, we are most interested in :

func numberOfSections(in tableView: UITableView) -> Int

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

These two function will command the content of your tableview. the first function to be invoqued is :

func numberOfSections(in tableView: UITableView) -> Int

Depending on what you return, it will invoque the second method, if you want your tableview to be empty all together then you can return 0 for the number if sections thus the other function won't even get invoqued.

if you return 5 for the numberOfSections then you ll get the other function invoqued as much, and you can return as much rows as you want depending on the section index argument that you get.

Here is an example of 5 sections with the thrid one being empty and all the rest having only one row :

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

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

    return 1
}

Upvotes: 1

Related Questions