简体   繁体   English

不调用BEMCheckBox委托

[英]BEMCheckBox delegate is not called

In my iOS Switch app I have a BEMCheckBox in each table cell. 在我的iOS Switch应用中,每个表格单元格中都有一个BEMCheckBox When dequeuing a cell I want to set a delegate that gets called. 使单元出队时,我想设置一个被调用的委托。

My problem is that the checkbox works fine but the delegate is not never called. 我的问题是该复选框可以正常工作,但永远不会调用该委托。 How to add a delegate to each checkbox? 如何将委托添加到每个复选框?

I want to know which indexPath for checkbox. 我想知道哪个indexPath用于复选框。 The plan is to pass model object to the delegate and update it accordingly. 计划是将模型对象传递给委托并相应地对其进行更新。

Table cell 表单元

let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
cell.doneCheckbox.delegate = DoneBEMCheckBoxDelegate()
return cell

Delegate is very simple 委托很简单

class DoneBEMCheckBoxDelegate: NSObject, BEMCheckBoxDelegate {

    @objc func didTap(_ checkBox: BEMCheckBox) {
        print("Checkbox tapped")
    }

}

cell.doneCheckbox.delegate = DoneBEMCheckBoxDelegate() is creating a new DoneBEMCheckBoxDelegate object in a local variable and assigning that as the delegate. cell.doneCheckbox.delegate = DoneBEMCheckBoxDelegate()在局部变量中创建一个新的DoneBEMCheckBoxDelegate对象,并将其分配为委托。 Since the delegate property is weak, it will be released as soon as the function exits because there is no strong reference remaining. 由于委托属性是弱的,因此将在函数退出时立即释放它,因为没有剩余的强引用。

I would suggest that having a separate object class to be the delegate probably isn't what you want anyway. 我建议拥有一个单独的对象类作为委托可能不是您想要的。

I would set the cell to be the check box delegate and then declare another protocol so that the cell can have its own delegate, which would be your table view controller. 我将单元格设置为复选框委托,然后声明另一个协议,以便该单元可以拥有自己的委托,这将是您的表视图控制器。

protocol MyCellDelegate {
    func checkBox(for cell: MyCell, isOn: Bool)
}

class MyCell: UITableViewCell, DoneBEMCheckBoxDelegate {

    var delegate: MyCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        self.doneCheckBox.delegate = self
    }

    @objc func didTap(_ checkBox: BEMCheckBox) {
        print("Checkbox tapped")
        self.delegate?.checkBox(for: self, isOn: checkBox.isOn)
    }

}


class YourViewController: MyCellDelegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        ...
        cell.delegate = self

        return cell
    }

    func checkBox(for cell: MyCell, isOn: Bool) {

        guard let indexPath = tableView.indexPath(for: cell) else {
            return
        }
        // Now do whatever you need to with indexPath
    }
}

This way you avoid creating additional objects and datastructures and you won't have a problem if cells are re-ordered as there is no dependency on index path. 这样,您可以避免创建其他对象和数据结构,并且如果对单元格进行重新排序不会有问题,因为对索引路径没有依赖性。

I noticed that delegate is a weak reference in checkbox class, as it is supposed to be :) So my delegate was freed after method scope ended. 我注意到委托在checkbox类中是一个弱引用,因为它应该是:)因此,在方法作用域结束后,我的委托被释放了。

I fixed this by storing delegates in view controller during their usage. 我通过在使用过程中将委托存储在视图控制器中来解决此问题。

var checkboxDelegates: [IndexPath:DoneBEMCheckBoxDelegate] = [:]
...
let checkboxDelegate = DoneBEMCheckBoxDelegate(realm: realm, set: set)
checkboxDelegates[indexPath] = checkboxDelegate
cell.doneCheckbox.delegate = checkboxDelegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM