gpt4 book ai didi

swift - 使用 UISwitch 显示/隐藏表格 View 单元格

转载 作者:行者123 更新时间:2023-11-28 13:47:02 25 4
gpt4 key购买 nike

我想使用 UISwitch 在动态 TableView 中显示/隐藏 tableViewCell。UISwitch 定义在 UITableViewCell 类中。

@IBOutlet weak var switchState: UISwitch!

在另一个文件中我想说如果这个开关打开,行数将是 5 否则它应该是 4

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let test = PopupViewCell()
if test.switchState?.isOn == true {
detailsTableView.reloadData()
return 5
} else {
return 4
}
}

但它不工作,它总是读取 `return 4。我也测试了一下:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let test = PopupViewCell()
if test.switchState.isOn {
detailsTableView.reloadData()
return 5
} else {
return 4
}
}

但是我会得到这个错误:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

我不确定在这个过程之前我是否必须使用一些 Action 函数,如果有人能帮助我,我将不胜感激。

最佳答案

最适合初学者的方法是委托(delegate)。当 Switch 单元检测到 .valueChanged 事件时,它应该将此转发给委托(delegate)。委托(delegate)依次更新其是否显示开关的模型,然后重新加载 tableView

这是一个 Playground 示例:

import UIKit
import PlaygroundSupport

protocol SwitchDelegate: class {
func toggle(isOn: Bool)
}

class SwitchCell: UITableViewCell {
private lazy var switchControl: UISwitch = {
let switchControl = UISwitch()
contentView.addSubview(switchControl)
switchControl.translatesAutoresizingMaskIntoConstraints = false
switchControl.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 12).isActive = true
switchControl.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
switchControl.addTarget(self, action: #selector(toggleSwitch(_:)), for: .valueChanged)
return switchControl
}()
private weak var delegate: SwitchDelegate?
override func awakeFromNib() {
super.awakeFromNib()

}
func configure(isOn: Bool, delegate: SwitchDelegate) {
switchControl.isOn = isOn
self.delegate = delegate
}
@objc private func toggleSwitch(_ sender: UISwitch) {
delegate?.toggle(isOn: sender.isOn)
}
}

class ViewController: UITableViewController {
private let data = (0..<5).map { $0 + 1 }
private var isOn = true
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: String(describing: UITableViewCell.self))
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count + (isOn ? 1 : 0)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if isOn && indexPath.row == 0 {
let switchCell = SwitchCell(style: .default, reuseIdentifier: String(describing: SwitchCell.self))
switchCell.configure(isOn: isOn, delegate: self)
return switchCell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UITableViewCell.self), for: indexPath)
let dataIndex = indexPath.row - (isOn ? 1 : 0)
cell.textLabel?.text = String(describing: data[dataIndex])
return cell
}
}
}

extension ViewController: SwitchDelegate {
func toggle(isOn: Bool) {
self.isOn = isOn
tableView.reloadData()
}
}

PlaygroundPage.current.liveView = ViewController()

关于swift - 使用 UISwitch 显示/隐藏表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55288933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com