如何解决当开关快速打开/关闭时显示/隐藏 TableViewCell 行
我希望在打开开关时在一个部分中显示额外的 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 举报,一经查实,本站将立刻删除。