ios - 带有协议(protocol)的自定义 Cell,用于检查 UISwitch 中的更改

标签 ios swift uitableview uiswitch

我创建了一个自定义单元格 和一个协议(protocol) 以了解UISwitch 上的更改。我想我拥有我需要的一切,但我无法触发 func didChangeSwitchState

VouchersViewController.swift

// MARK: - UITableViewDelegate

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:VouchersFilterCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! VouchersFilterCell
    cell.delegate = self
    cell.textLabel?.text = self.vouchersFilters[indexPath.row]

    return cell
}

// MARK: - VouchersFilterCellDelegate

func didChangeSwitchState(sender: VouchersFilterCell, isOn: Bool) {
    let indexPath = self.tableView.indexPath(for: sender)
    debugPrint(indexPath?.row)
}

BaseCell.swift

class BaseCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // code common to all your cells goes here
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    }

    func setupViews(){
    }
}

VouchersFilterCell.swift

protocol VouchersFilterCellDelegate: class {
    func didChangeSwitchState(sender: VouchersFilterCell, isOn: Bool)
}

class VouchersFilterCell: BaseCell {

    // MARK: - UIComponent

    let titleLabel = UILabel()
    let filterSwitch: UISwitch = {
        let fSwitch = UISwitch()
        fSwitch.addTarget(self, action: #selector(handledSwitchChange(sender:)), for: .valueChanged)
        return fSwitch
    }()

    weak var delegate: VouchersFilterCellDelegate?

    // MARK: - Life Cycle    

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    // MARK: - Setup

    override func setupViews() {
        addSubview(titleLabel)
        addSubview(filterSwitch)

        addConstraintsWithFormat(format: "V:|-13-[v0]-13-|", views: titleLabel)
        addConstraintsWithFormat(format: "H:|-16-[v0]-16-[v1]-16-|", views: titleLabel, filterSwitch)
        addConstraintsWithFormat(format: "V:|-7-[v0]", views: filterSwitch)
    }

    // MARK: - IBAction 

    @IBAction func handledSwitchChange(sender: UISwitch) {
        self.delegate?.didChangeSwitchState(sender: self, isOn:filterSwitch.isOn)
    }
}

最佳答案

正如我们在评论中一起发现的那样,似乎在 self 准备好之前创建了一个 let 属性,因此 Switch 目标为 nil。更改为惰性 var 可确保首先创建 self。

lazy var filterSwitch: UISwitch = {
    let fSwitch = UISwitch()
    fSwitch.addTarget(self, action: #selector(handledSwitchChange(sender:)), for: .valueChanged)
    return fSwitch
}()

关于ios - 带有协议(protocol)的自定义 Cell,用于检查 UISwitch 中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43478252/

相关文章:

ios - Xcode 和 Unity 缺少库 'lGoogleUtilities'

ios - 在 Xcode 上创建重复的 outlets/actions

arrays - 用一个条件过滤多个数组

ios - 将单元格信息从一个 View Controller 发送到另一个 Swift 和 Parse

ios - UITableview 委托(delegate)方法未执行

ios - TableView 自定义单元格的麻烦

iOS CMStepCounter & CMMotionActivityManager

ios - Restkit .20 中的删除对象未采用 JSON 值

ios - 在 UIButton 上找到 touchDragInside 发送事件的方向

ios - 快速自动滚动到 TextView 控件的顶部