(Swift) 如何在切换开关打开时隐藏 tableView 中的某些部分? [英] (Swift) How to hide some sections in tableView when toggle switch is on?
问题描述
我有 5 个 tebleView 部分,每个部分的代码基本相同.主要区别在于 cell.labelCell.text
:
I have 5 tebleView sections, code is mostly the same for each of them. Main difference is in 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 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 为 ON 时,可以这样做吗?
Question : If I have 5 sections like this and I want to hide for example the first one when toggleSwitch is ON in the last one, is it possible to do it?
推荐答案
方法:
在您的 Model
中,您可以创建一个 isHidden
属性,该属性将跟踪该部分是否应该隐藏,即
In your Model
, you can create a isHidden
property that will keep a track of whether the section should be hidden or not, i.e.
class Model {
var sectionName: String
var isHidden = false
init(sectionName: String) {
self.sectionName = sectionName
}
}
现在,将 UITableViewDataSource
方法修改为,
Now, modify the UITableViewDataSource
methods to,
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(_:)
中的闭包 callback?()
并且隐藏/取消隐藏将被自动处理.
And you can simply call the closure callback?()
in toggleSwitch(_:)
and the hiding/unhiding will be handled automatically.
class TableViewCell: UITableViewCell {
@IBOutlet weak var labelCell: UILabel!
@IBOutlet weak var cellSwitch: UISwitch!
var callback:(()->())?
@IBAction func toggleSwitch(_ sender: Any) {
callback?()
}
}
这篇关于(Swift) 如何在切换开关打开时隐藏 tableView 中的某些部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!