Ios (Swift)当切换开关打开时,如何隐藏tableView中的某些部分?

Ios (Swift)当切换开关打开时,如何隐藏tableView中的某些部分?,ios,swift,uitableview,toggle,Ios,Swift,Uitableview,Toggle,我有5个tebleView部分,每个部分的代码基本相同。主要区别在于cell.labelCell.text: CellForRow方法: 单元格的类别: class ToggleTableViewCell: UITableViewCell { @IBOutlet weak var labelCell: UILabel! @IBOutlet weak var cellSwitch: UISwitch! var callback:((Bool) -> Void)?

我有5个tebleView部分,每个部分的代码基本相同。主要区别在于cell.labelCell.text:

CellForRow方法:

单元格的类别:

class ToggleTableViewCell: UITableViewCell {

    @IBOutlet weak var labelCell: UILabel!

    @IBOutlet weak var cellSwitch: UISwitch!

    var callback:((Bool) -> Void)?

    @IBAction func toggleSwitch(_ sender: Any) {
        if cellSwitch.isOn == true {
            callback?(true)
        }
        else {
            callback?(false)
        }
    }
}
问题:如果我有5个类似的部分,并且我想在最后一个部分中打开toggleSwitch时隐藏第一个部分,是否可以这样做?

方法:

在模型中,您可以创建一个isHidden属性,该属性将跟踪是否应隐藏剖面,即

class Model {
    var sectionName: String
    var isHidden = false

    init(sectionName: String) {
        self.sectionName = sectionName
    }
}
现在,将UITableViewDataSource方法修改为

class VC: UIViewController, UITableViewDataSource, UITableViewDelegate {
    let arr = [Model(sectionName: "Section 1"), Model(sectionName: "Section 2"), Model(sectionName: "Section 3"), Model(sectionName: "Section 4"), Model(sectionName: "Section 5")]
    lazy var dataSource = self.arr

    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.count
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        cell.cellSwitch.setOn(false, animated: false)
        cell.labelCell.text = dataSource[indexPath.section].sectionName
        cell.callback = {[weak self] in
            guard let `self` = self else {
                return
            }
            self.arr[indexPath.section].isHidden = !(self.arr[indexPath.section].isHidden)
            self.dataSource = self.arr.filter({ !$0.isHidden })
            tableView.reloadData()
        }
        return cell
    }
}
你可以简单地调用闭包回调?在toggleSwitch\中,隐藏/取消隐藏将自动处理

class TableViewCell: UITableViewCell {
    @IBOutlet weak var labelCell: UILabel!
    @IBOutlet weak var cellSwitch: UISwitch!

    var callback:(()->())?
    @IBAction func toggleSwitch(_ sender: Any) {
        callback?()
    }
}

我不知道您想显示什么,但如果问题是能否显示/隐藏tableView的一部分,则答案是“是”

为此,您需要将视图与要显示的数据分开

//让我们在某处宣布这一点 //假设顶部数组是节数,内部数组是行数 var表格数据:[[String]=[[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]] //tableView数据源 集合视图中的func NumberOfSection:UICollectionView->Int{ return tableData.count } func tableView utableview:UITableView,numberofrowsinssection:Int->Int{ 返回tableData[节].count } func tableView u tableView:UITableView,cellforrow在indexPath:indexPath->UITableView单元格{ 让cell=tableView.dequeueReusableCellwithIdentifier:ToggleTableViewCell,for:indexPath as!ToggleTableViewCell cell.cellSwitch.setOnfalse,已设置动画:false let data=tableData[indexPath.section] cell.labelCell.text=数据[indexath.row] cell.callback={[unowned self]签入 //从选择区域中删除数据 //因此,如果删除第2节,[7,8,9]将被删除 //自动取款机,它只是删除,但当你决定你真正想做什么,然后你可以玩周围,并实施切换操作 tableData.removeat:indexPath.section UIView.transitionwith:tableView, 持续时间:0.5, 选项:。ShowHideTransitionView, 动画:{self.tableView.reloadData} } }
您好,我想您必须研究行的高度方法以及切换操作,需要执行tableview.beginupdate和tableview.endupdate。谢谢,您有任何关于这些内容的ceode示例或有用的帖子/博客吗?是的,当然。请检查以下内容:
class TableViewCell: UITableViewCell {
    @IBOutlet weak var labelCell: UILabel!
    @IBOutlet weak var cellSwitch: UISwitch!

    var callback:(()->())?
    @IBAction func toggleSwitch(_ sender: Any) {
        callback?()
    }
}