我有一个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]
}
}
}