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
// LEAK
I have edited, checking for nil before assigning. And I am calling initializeViews inside configure. But still seeing the leak.