在collectionView_:cellForItemAt :)内部的闭包是否需要弱自身?

问题描述

编辑:我检查了类似的问题,但是通过了self

var buttonAction: ((UITableViewCell) -> Void)?

@IBAction func buttonPressed(_ sender: Any) {
     buttonAction?(self)
}

我什么也没传递


我有UICollectionView个充满UITextField的单元格。 这是我的手机的样子:

class SearchCell: UICollectionViewCell {
    @IBOutlet weak var textField: UITextField!
    var startedEditing: (() -> Void)?
    var endedEditing: (() -> Void)?
}
extension SearchCell: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        startedEditing?()
    }
    func textFieldDidEndEditing(_ textField: UITextField,reason: UITextField.DidEndEditingReason) {
        endedEditing?()
    }
}

我正在将UITextField委托分配给SearchCell。每当调用委托函数时,我都会调用闭包。

然后我将print语句添加到collectionView(_:cellForItemAt:)内的闭包中:

extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell",for: indexPath) as? SearchCell {
            cell.startedEditing = { [weak self] in /// do I need weak self here?
                print("\(indexPath) started editing")
            }
            cell.endedEditing = { [weak self] in /// or here?
                print("\(indexPath) ended editing")
            }
        }
    }
}

我的问题是,封包内是否需要[weak self]?目前,我什至没有引用self(我刚得到indexPath),Xcode说“变量'self'被写入,但从未读取。”但是,当前不是collectionView(_:cellForItemAt:)SearchCellcollectionView(_:cellForItemAt:)的流-似乎它捕获了某物 ...

应该是[weak collectionView]吗?或者,如果我将其更改为:

cell.startedEditing = { [weak self] in
    print("\(indexPath) started editing")
    self?.callSomeFunction()
}

[weak self]在这里合适吗?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...