问题描述
编辑:我检查了类似的问题,但是通过了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:)
→SearchCell
→collectionView(_:cellForItemAt:)
的流-似乎它捕获了某物 ...
应该是[weak collectionView]
吗?或者,如果我将其更改为:
cell.startedEditing = { [weak self] in
print("\(indexPath) started editing")
self?.callSomeFunction()
}
[weak self]
在这里合适吗?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)