【问题标题】:Set target for switch in custom UITableViewCell在自定义 UITableViewCell 中设置切换目标
【发布时间】:2018-07-23 01:30:54
【问题描述】:

我有一个带开关的自定义单元格。我想在tableView(cellForRowAt:) 中添加目标。但是通过这样做(在示例代码中),我将创建一个强大的引用循环。

然后我尝试使用协议/委托方法,但这意味着所有单元格都会调用相同的方法。

如何设置我的单元格并相应地添加目标?


强引用循环:

class customCell: UITableViewCell {
    var customSwitch: UISwitch()

    // setup switch
}

class VC1: UITableViewController {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell

            cell.customSwitch.addTarget(self, action: #selector(handleSwitch1), for: valueChanged)

            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell

            cell.customSwitch.addTarget(self, action: #selector(handleSwitch2), for: valueChanged)

            return cell
        default:
            fatalError()
        }
    }

    @objc func handleSwitch1(_ sender: UISwitch) { }
    @objc func handleSwitch2(_ sender: UISwitch) { }
}

使用委托:

class customCell: UITableViewCell {
    var customSwitch: UISwitch()
    weak var delegate: VC1Delegate?

    // setup switch

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        customSwitch.addTarget(self, action: #selector(handleSwitch), for: valueChanged)
    }

    @objc func handleSwitch(_ sender: UISwitch) {
        delegate?.handleSwitch1(sender)
    }
}

protocol VC1Delegate: class {
    func handleSwitch1(_ sender: UISwitch)
    func handleSwitch2(_ sender: UISwitch)
}

class VC1: UITableViewController, VC1Delegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell

            cell.delegate = self

            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell

            cell.delegate = self

            return cell
        default:
            fatalError()
        }
    }

    @objc func handleSwitch1(_ sender: UISwitch) { }
    @objc func handleSwitch2(_ sender: UISwitch) { }
}

【问题讨论】:

    标签: swift uitableview memory-leaks delegates uiswitch


    【解决方案1】:

    在您标记为“强引用循环”的代码中,没有强引用循环,所以继续使用它。

    您认为强参考周期在哪里?是使用self 作为target 吗? docs 明确表示:

    控件不保留目标参数中的对象

    这只是意料之中的事。如果您不能将 self 设置为控件的操作目标,我们都会陷入困境。

    【讨论】:

    • 谢谢。强引用循环可能是由我的代码中的其他内容引起的。我的错误,我认为这是造成它的原因。重新开始挖掘!
    • 无需挖掘,如果您有泄漏,您可以立即找出它是什么。使用 Memory Graph 或 Leaks 工具。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多