swift - 在自定义 UITableViewCell 中设置切换目标

标签 swift uitableview memory-leaks delegates uiswitch

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

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

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

<小时/>

强引用循环:

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) { }
}
<小时/>

使用委托(delegate):

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

最佳答案

在标记为“强引用循环”的代码中,没有强引用循环,因此请继续使用它。

你想象中的强引用循环在哪里?是使用self作为目标吗? docs明确地说:

The control does not retain the object in the target parameter

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

关于swift - 在自定义 UITableViewCell 中设置切换目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51470576/

相关文章:

json - 创建 JSON 响应

ios - trailingSwipeActionsConfigurationForRowAt 不能稳定工作

swift - iOS 应用程序扩展 - 操作 - 自定义数据

ios - AWS Cognito Swift SDK 如何检查用户是否已确认?

swift - 协议(protocol)限制会导致代码执行崩溃

iphone UITableView 自定义单元格选定的图像

ios - UITableView/UIScrollView 改变手指抬起后弹跳的速度

ios - performSelector可能会导致泄漏,因为其选择器未知

jquery - jQPLOT - 内存泄漏

我根本找不到C内存泄漏