У меня есть UICollectionView, который имеет несколько разных источников данных, каждый из которых представляет собой массив UIViews.Но я замечаю, что когда я меняю источник данных и перезагружаю представление сбора, использование памяти не остается одинаковым или даже снижается, оно увеличивается.
Поэтому мой вопрос заключается в том, как очистить используемую память?Я попытался установить источник данных для пустого массива и вызвать collectionView.reloadData()
, но это все равно не очистило память.
Спасибо!
Код, который я использую:
var allThumbArrays = [
templatesSection1Small,
templatesSection2Small,
templatesSection3Small
]
class sectionsMenuLabels {
static func section1Button() -> UIButton {
let myButton = UIButton()
myButton.setTitle("One", for: .normal)
myButton.setTitleColor(colours.darkbg, for: .normal)
myButton.isUserInteractionEnabled = false
return myButton
}
static func section2Button() -> UIButton {
let myButton = UIButton()
myButton.setTitle("Two", for: .normal)
myButton.setTitleColor(colours.darkbg, for: .normal)
myButton.isUserInteractionEnabled = false
return myButton
}
static func section3Button() -> UIButton {
let myButton = UIButton()
myButton.setTitle("Three", for: .normal)
myButton.setTitleColor(colours.darkbg, for: .normal)
myButton.isUserInteractionEnabled = false
return myButton
}
}
let sectionMenuButtonsArray: [UIButton] = [
sectionsMenuLabels.section1Button(),
sectionsMenuLabels.section2Button(),
sectionsMenuLabels.section3Button()
]
let tWidth = ((UIScreen.main.bounds.width - 20) / 3) - 15
let tHeight = (tWidth / 9) * 16
let tCollectionWidth = UIScreen.main.bounds.width - 20
static func templatesSection() -> UICollectionView {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 60, right: 10)
layout.itemSize = CGSize(width: tWidth, height: tHeight)
let myCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: tCollectionWidth, height: UIScreen.main.bounds.height), collectionViewLayout: layout)
return myCollectionView
}
static func sectionsMenuView() -> UICollectionView {
let sectionsMenuLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
sectionsMenuLayout.scrollDirection = .horizontal
sectionsMenuLayout.sectionInset = UIEdgeInsets(top: 5, left: 15, bottom: 0, right: 15)
sectionsMenuLayout.itemSize = CGSize(width: 110, height: 25)
sectionsMenuLayout.minimumInteritemSpacing = 10
let sectionsMenu = UICollectionView(frame: CGRect(x: 0, y: 0, width: (screenSizes.screenWidth - 20), height: 35), collectionViewLayout: sectionsMenuLayout)
return sectionsMenu
}
//View controller File
class TemplatesViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
let sectionsMenuView = sectionsMenuView()
let templatesSection = templatesSection()
let currentSectionShownNumber = 0
var thumbsToUse: [UIView] = []
override func viewDidLoad() {
super.viewDidLoad()
//MARK: sections menu collection view
sectionsMenuView.dataSource = self
sectionsMenuView.delegate = self
sectionsMenuView.backgroundColor = UIColor.clear
sectionsMenuView.register(sectionsMenuCell.self, forCellWithReuseIdentifier: "SectionsMenuCell")
self.view.addSubview(sectionsMenuView)
sectionsMenuView.translatesAutoresizingMaskIntoConstraints = false
sectionsMenuView.widthAnchor.constraint(equalToConstant: screenSizes.screenWidth).isActive = true
sectionsMenuView.heightAnchor.constraint(equalToConstant: 40).isActive = true
sectionsMenuView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
sectionsMenuView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 40).isActive = true
//MARK: templates section collection view
templatesSection.dataSource = self
templatesSection.delegate = self
templatesSection.backgroundColor = UIColor.clear
templatesSection.register(TemplateViewCell.self, forCellWithReuseIdentifier: "SectionCell")
self.view.addSubview(templatesSection)
templatesSection.translatesAutoresizingMaskIntoConstraints = false
templatesSection.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
templatesSection.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
templatesSection.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 20).isActive = true
templatesSection.heightAnchor.constraint(equalToConstant: 150).isActive = true
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.sectionsMenuView {
return sectionMenuButtonsArray.count
} else {
return thumbsToUse.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.sectionsMenuView {
let sectionsMenuCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SectionsMenuCell", for: indexPath as IndexPath)
sectionsMenuCell.addSubview(sectionMenuButtonsArray[indexPath.row])
return sectionsMenuCell
} else {
let section1Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SectionCell", for: indexPath as IndexPath)
//thumbs array
thumbsToUse = allThumbArrays[currentSectionShownNumber]
//add subview
let viewToAddToCell = (thumbsToUse[indexPath.row])
sectionCell.addSubview(viewToAddToCell)
return section1Cell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.sectionsMenuView {
currentSectionShownNumber = indexPath.row
thumbsToUse.removeAll()
templatesSection.reloadData()
thumbsToUse = allThumbArrays[currentSectionShownNumber]
templatesSection.reloadData()
} else {
chosenTemplateNumber = indexPath.row
chosenTemplate = thumbsToUse[chosenTemplateNumber]
}
}
}