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

(Swift) How to hide some sections in tableView when toggle switch is on?

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

CellForRow方法:

let cell = tableView.dequeueReusableCell(withIdentifier: "ToggleTableViewCell", for: indexPath) as! ToggleTableViewCell
            cell.cellSwitch.setOn(false, animated: false)

            switch (indexPath.section, indexPath.row) {
            case (1,0):
                cell.labelCell.text = "Section 1"

                cell.callback = { [unowned self] check in
                    UIView.transition(with: tableView,
                                      duration: 0.5,
                                      options: .showHideTransitionViews,
                                      animations: { self.tableView.reloadData() })
                }

单元格的 class:

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打开时隐藏第一个部分,是否可以这样做?

方法:

在您的 Model 中,您可以创建一个 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({ ![=11=].isHidden })
            tableView.reloadData()
        }
        return cell
    }
}

您只需在 toggleSwitch(_:) 中调用闭包 callback?(),hiding/unhiding 将自动处理。

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

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

我不知道你想显示什么,但是如果问题是“你能show/hide tableView的一部分吗?”答案是

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

// let this be declared somewhere
// assume that the top array will be the number of section and inner one will be number fo rows
var tableData: [[String]] = [["1","2","3"], ["4","5","6"], ["7","8","9"], ["10","11","12"], ["13","14","15"]]

// tableView dataSource

func numberOfSections(in collectionView: UICollectionView) -> Int {
   return tableData.count
}

func tableView( _ tableView: UITableView, numberOfRowsInSection section: Int ) -> Int {
   return tableData[section].count
}

func tableView( _ tableView: UITableView, cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "ToggleTableViewCell", for: indexPath) as! ToggleTableViewCell
   cell.cellSwitch.setOn(false, animated: false)
   let data = tableData[indexPath.section]
   cell.labelCell.text = data[indexPath.row]
   cell.callback = { [unowned self] check in 
      //removes the data from the selection section
      // so if you remove section 2, ["7","8","9"] will be remove
      // atm, it just removes but when you decide what you actually want to do then you can play around and implement a toggling action
      tableData.remove(at: indexPath.section)
      UIView.transition(with: tableView,
                        duration: 0.5,
                        options: .showHideTransitionViews,
                        animations: { self.tableView.reloadData() })
   }
}