如何修复从调试存储器图xcode中的内存泄漏
我正在尝试通过调试内存图和struct在此处修复内存泄漏。
我的代码在uicollectionView中,cellforitemat indexpath
如下
guard let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? DiscoveryCell else {
Log.error("DiscoveryCell not registered")
return UICollectionViewCell()
}
let buttonView = self.delegate?.getBottomView(template: viewModel)
cell.configure(model: viewModel, buttonView: buttonView)
return cell
,自定义单元包含
class DiscoveryCell: UICollectionViewCell, Identifiable {
private(set) var viewModel: DiscoveryProtocol?
@IBOutlet private var buttonContainerView: UIView!
func configure(model: DiscoveryProtocol, buttonView: UIView?) {
guard let viewModel = model as? DiscoveryProtocol else {
return
}
self.viewModel = viewModel
if let actionButtonView = buttonView {
actionButtonView.frame = buttonContainerView.bounds
self.buttonContainerView.addSubview(actionButtonView)
}
}
override func prepareForReuse() {
super.prepareForReuse()
self.buttonContainerView.subviews.forEach { subview in
subview.removeFromSuperview()
}
}
}
我的代码有什么问题?我的意思是在哪里找到内存泄漏
Guys I'm trying to fix memory leak through Debug Memory Graph and struct here.
My code in UICollectionView, cellForItemAt indexPath
is as follow
guard let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? DiscoveryCell else {
Log.error("DiscoveryCell not registered")
return UICollectionViewCell()
}
let buttonView = self.delegate?.getBottomView(template: viewModel)
cell.configure(model: viewModel, buttonView: buttonView)
return cell
and custom cell contains
class DiscoveryCell: UICollectionViewCell, Identifiable {
private(set) var viewModel: DiscoveryProtocol?
@IBOutlet private var buttonContainerView: UIView!
func configure(model: DiscoveryProtocol, buttonView: UIView?) {
guard let viewModel = model as? DiscoveryProtocol else {
return
}
self.viewModel = viewModel
if let actionButtonView = buttonView {
actionButtonView.frame = buttonContainerView.bounds
self.buttonContainerView.addSubview(actionButtonView)
}
}
override func prepareForReuse() {
super.prepareForReuse()
self.buttonContainerView.subviews.forEach { subview in
subview.removeFromSuperview()
}
}
}
what's wrong with my code? I mean where do I find the memory leaks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论