Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未调用BEMCheckBox委托_Ios_Swift_Uitableview_Delegates - Fatal编程技术网

Ios 未调用BEMCheckBox委托

Ios 未调用BEMCheckBox委托,ios,swift,uitableview,delegates,Ios,Swift,Uitableview,Delegates,在我的iOS Switch应用程序中,每个表单元格中都有一个。当对单元格进行排队时,我想设置一个被调用的委托 我的问题是,复选框工作正常,但从未调用过委托。如何向每个复选框添加代理 我想知道复选框的哪个索引。计划是将模型对象传递给代理并相应地更新它 表单元格 let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) cell.doneCheckbox.delegate = DoneBEMC

在我的iOS Switch应用程序中,每个表单元格中都有一个。当对单元格进行排队时,我想设置一个被调用的委托

我的问题是,复选框工作正常,但从未调用过委托。如何向每个复选框添加代理

我想知道复选框的哪个索引。计划是将模型对象传递给代理并相应地更新它

表单元格

let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
cell.doneCheckbox.delegate = DoneBEMCheckBoxDelegate()
return cell
委托很简单

class DoneBEMCheckBoxDelegate: NSObject, BEMCheckBoxDelegate {

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

}

我注意到委托在类中是一个弱引用,正如它应该的那样:所以我的委托在方法作用域结束后被释放

我通过在代理使用期间将代理存储在视图控制器中修复了此问题

var checkboxDelegates: [IndexPath:DoneBEMCheckBoxDelegate] = [:]
...
let checkboxDelegate = DoneBEMCheckBoxDelegate(realm: realm, set: set)
checkboxDelegates[indexPath] = checkboxDelegate
cell.doneCheckbox.delegate = checkboxDelegate
cell.doneCheckbox.delegate=DoneBEMCheckBoxDelegate正在局部变量中创建一个新的DoneBEMCheckBoxDelegate对象,并将其指定为委托。由于委托属性很弱,因此函数退出后将立即释放它,因为没有强引用

我建议,无论如何,拥有一个单独的对象类作为委托可能不是您想要的

我会将单元格设置为复选框委托,然后声明另一个协议,以便单元格可以有自己的委托,即您的表视图控制器

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
    }
}

这样可以避免创建额外的对象和数据结构,如果单元格被重新排序,则不会出现问题,因为对索引路径没有依赖关系

由于委托和单元在此答案中是同一对象,因此获得不同的生命周期没有问题: