问题描述
我希望在打开开关时在一个部分中显示额外的 tableview 行,并在关闭同一开关时让这些单元格消失。我一直在玩一堆代码,但没有任何工作正常,所以此时我不知道该怎么做。感谢您的指导。
import UIKit
protocol SwitchDelegate {
func toggle(isOn: Bool)
}
class CustomCell: UITableViewCell {
static let identifier = "CustomCell"
var cellLabel: UILabel = {
let cellLabel = UILabel()
return cellLabel
}()
var cellSwitch: UISwitch = {
let cellSwitch = UISwitch()
cellSwitch.addTarget(self,action: #selector(toggleSwitch),for: UIControl.Event.valueChanged)
return cellSwitch
}()
@objc private func toggleSwitch(_ sender: UISwitch) {
delegate?.toggle(isOn: sender.isOn)
}
}
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var switchDelegate: SwitchDelegate!
public var tableView: UITableView = {
var tableView = UITableView(frame: .zero,style: .insetGrouped)
tableView.register(CustomCell.self,forCellReuseIdentifier: CustomCell.identifier)
return tableView
}()
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.identifier,for: indexPath) as? CustomCell else { return UITableViewCell() }
cell.configure(text: "Cell Name")
cell.selectionStyle = .none
return cell
}
extension ViewController: SwitchDelegate {
func toggle(isOn: Bool) {
tableView.reloadData()
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)