【发布时间】:2018-02-15 14:31:00
【问题描述】:
class CView: UIView {
// MARK: - class
class func nibView(frame: CGRect, assertion: Assertion?,contentReference: ContentReference?, delegate : AssertionViewDelegate? = nil) -> CView {
let view = UINib(nibName: "CView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CView // LEAK
view.delegate = delegate
if let assertion = assertion {
view.configure(for: assertion, contentReference: contentReference)
}
return view
}
}
在 UITableViewCell 中,我在 init 中添加了这个视图
func initializeViews(){
if cview == nil {
self.isOpaque = true
let view = CView.nibView(frame: CGRect(x: 0, y: 0, width: Int(self.bounds.width), height: 0), assertion: nil, contentReference: nil)
self.contentView.addSubview(view)
self.cview = view
self.cview.translatesAutoresizingMaskIntoConstraints = false
self.cview.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
self.cview.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
self.cview.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
self.cview.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
}
}
func configureAssertion(){
initializeViews()
assertionView.tag = self.tag
self.cview.configure() // configure with model
}
这会造成泄漏 - let view = UINib(nibName: "CView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CView // 泄漏
I have edited, checking for nil before assigning. And I am calling initializeViews inside configure. But still seeing the leak.
【问题讨论】:
-
你的问题是什么?另外,您认为为什么会造成泄漏?
-
仪器泄漏分析显示此处存在内存泄漏。 @mag_zbc
-
单元格被重复使用...如果您每次请求单元格时都调用
initializeViews(),看起来您正在添加另一个子视图每次。
标签: ios memory-leaks automatic-ref-counting awakefromnib uinib