Ios 在运行时使用swift在UITableView中添加/删除节

Ios 在运行时使用swift在UITableView中添加/删除节,ios,swift,uitableview,runtime,Ios,Swift,Uitableview,Runtime,我正在尝试使用swift创建一个具有分组样式的tableView,如下图所示 override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! { var label : UILabel = UILabel() label.text = self.sectionHeaders[section] return label } overr

我正在尝试使用swift创建一个具有分组样式的tableView,如下图所示

override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {

    var label : UILabel = UILabel()
    label.text = self.sectionHeaders[section]
    return label
}

override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {

    var label : UILabel = UILabel()
    label.text = self.sectionHeaders[section]
    return label
}
我如何处理以下问题:

override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {

    var label : UILabel = UILabel()
    label.text = self.sectionHeaders[section]
    return label
}
  • 我需要在运行时在表中添加或删除节和CellView。第一部分,即食物将始终保持不变,但其他部分是动态添加或删除的,如水果、蔬菜等

  • override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    
  • 我需要为每个新添加的节自定义节标题

  • override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    

    谢谢,

    要更新章节标题,您只需拥有一个包含所有标题的数组,然后将该数组更新为包含相应章节索引的正确标题

    override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    
    所以有一个实例变量,比如

    override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    
    var-sectionHeaders:String=[“食物”、“水果”、“蔬菜”]

    override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    
    然后执行此方法:

    override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    
    更新标题数组后,您可能希望更新或删除节。要做到这一点,请使用这两种方法

    override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    
    func insertSections(_ sections: NSIndexSet,withRowAnimation animation: UITableViewRowAnimation)
    

    override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    
    这种方法也可能派上用场

    override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    
        var label : UILabel = UILabel()
        label.text = self.sectionHeaders[section]
        return label
    }
    
    func moveSection(_ section: Int, toSection newSection: Int)