简体   繁体   English

iOS在TableView中创建空部分

[英]iOS Create Empty Section in TableView

I'm trying to master the use of TableViews, and I'm trying to populate one programmatically. 我试图掌握TableViews的使用,并且试图以编程方式填充其中一个。 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. 我可以看到reloadData()基本上是基于tableView函数重新制作表的,但是我想不出任何方法来制作一个部分,而在reloadData()调用期间也没有用行填充它。

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. TableView正在将其数据的知识委派给dataSource。

For that matter, the dataSource has to implement specific methods, in your case, we are most interested in : 为此,dataSource必须实现特定的方法,对于您而言,我们最感兴趣的是:

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. 根据您返回的内容,它会调用第二种方法,如果您希望表视图全部为空,则如果各节之间的数字相同,则您可以返回数字0,因此其他函数甚至不会被调用。

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. 如果为numberOfSections返回5,则将使另一个函数的调用次数增加,并且可以根据获取的section index参数返回任意多的行。

Here is an example of 5 sections with the thrid one being empty and all the rest having only one row : 这是5个部分的示例,其中第三个部分为空,其余所有部分仅一行:

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

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

    return 1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM