问题
I have 5 tebleView sections, code is mostly the same for each of them. Main difference is in cell.labelCell.text
:
CellForRow
method:
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() })
}
cell's 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)
}
}
}
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?
回答1:
Approach:
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
}
}
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
}
}
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?()
}
}
回答2:
I do not know what you want to display, but if the question is "Can you show/hide a section of a tableView" answer is YES
For this to work you need to separate your view with the data you want to display
// 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() })
}
}
来源:https://stackoverflow.com/questions/60414472/swift-how-to-hide-some-sections-in-tableview-when-toggle-switch-is-on