Ive created a CollectionView called and on each CollectionViewCell I have created a CollectionView. Below I have included the code for the collectionView which goes inside the cell of the MainCollectionView. Every time that I swipe through the cells from one collectionView to another the memory grows and after a cell is deleted the memory is not released. I've read that collectionView holds a strong reference to its layout Im not sure if that could be the cause. Am I implementing this wrong. thank you for you help.
MainViewController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FullPageCell.reuseIdentifier, for: indexPath) as? FullPageCell else{
assertionFailure("Fatal Error FullPageCell not dequed")
return UICollectionViewCell()
}
cell.backgroundColor = UIColor.green
cell.cellTappedDelegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfPages
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let cell = cell as? FullPageCell{
cell.setUpCollectionView()
}
}
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let cell = cell as? FullPageCell{
cell.collectionView = nil
}
}
SubCell CollectionView
static var reuseIdentifier: String = "FullPageCell"
var collectionView:UICollectionView? = nil
weak var cellTappedDelegate:CellTappedDelegate?
var image:UIImage = #imageLiteral(resourceName: "Image")
override init(frame: CGRect) {
super.init(frame:frame)
self.setUpCollectionView()
}
func setUpCollectionView(){
let view = self.contentView
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 1
layout.minimumInteritemSpacing = 1
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: (view.bounds.width - 2)/3, height: (view.bounds.width - 2)/3)
self.collectionView = UICollectionView(frame:view.bounds, collectionViewLayout: layout)
guard let collectionView = collectionView else{
return
}
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(SmallCell.self, forCellWithReuseIdentifier: SmallCell.reuseIdentifier)
collectionView.backgroundColor = UIColor.white
self.collectionView?.isPagingEnabled = true
view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SmallCell.reuseIdentifier, for: indexPath) as? SmallCell else{
assertionFailure("Fatal Error FullPageCell not dequed")
return UICollectionViewCell()
}
cell.backgroundColor = UIColor.yellow
cell.imageView.image = self.image
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1000
}