[英]Memory usage keeps increasing
我創建了一個非常簡單的測試應用程序。 它包括3個視圖控制器。 主視圖控制器具有一個使用自定義單元格的表視圖。 可以通過主視圖控制器訪問其他兩個視圖控制器,每個視圖控制器都有集合視圖,並且可以返回到主視圖控制器。
這是內存問題。 每當我單擊3個視圖控制器中的任何一個單元時,內存使用量都會增加。 我在使用“泄漏”分析模板時運行了該應用程序,但沒有發現泄漏。 還使用了“分配”配置文件模板,檢查了2個視圖控制器(記錄了引用計數),並釋放了我程序下所有已存儲的引用計數。
我一直無法使用Debug Memory Graph,因為它不斷使Xcode崩潰...
主視圖控制器
import UIKit
class TableViewController: UITableViewController, UISearchBarDelegate {
@IBOutlet weak var searchForTool: UISearchBar!
@IBOutlet weak var toolTable: UITableView!
var searchActive : Bool = false
var data = [" Alphabetical", " Numerical"]
var identities = ["A", "B"]
override func viewDidLoad() {
super.viewDidLoad()
toolTable.delegate = self
toolTable.dataSource = self
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.toolLabel.text = data[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
其他視圖控制器之一(兩者相同)
import UIKit
class AlphabeticalViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var labelArray = [String]()
var identities = [String]()
override func viewDidLoad() {
super.viewDidLoad()
labelArray = ["Main", "Definitions", "Steps", "References", "Other"]
identities = ["C", "B", "B", "D", "E"]
self.navigationController?.setNavigationBarHidden(true, animated: false)
collectionView.delegate = self
collectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return labelArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let myLabel = cell.viewWithTag(1) as! UILabel
myLabel.text = labelArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
自定義單元格類
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet weak var toolLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
如果需要,我將提供其他圖像。 任何幫助將不勝感激。
問題似乎出在CustomCell
內部,也許某些資源未初始化。 在awakeFromNib()
或setSelected(...)
是否有一些代碼?
您的tableView和collection視圖有問題。 看IBOutlets你的tableView名稱是toolTable
@IBOutlet weak var toolTable: UITableView!
但是在tableView的數據源中,您正在訪問錯誤的tableView
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// this is the wrong tableView
/*
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
*/
// you should use the tableview which you have declared
let cell = toolTable.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.toolLabel.text = data[indexPath.row]
return cell
}
您的CollectionViewControllers也有同樣的問題。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//accessing wrong collectionView
/*
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
*/
// here you have to use self because your have named your collectionView the same as collectionView
let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let myLabel = cell.viewWithTag(1) as! UILabel
myLabel.text = labelArray[indexPath.row]
return cell
}
注意:您的TableViewController和CollectionViewController已經是Controllers。 想知道為什么還要使用另一個tableView和collection視圖IBOutlets。 一次使用一個
首先,我要感謝@totiG指出問題可能出在導航控制器上,他是對的。
還有其他較小的內存問題,但迄今為止最大的問題與我的導航有關。 我一直將控制器推入導航堆棧,而沒有彈出它們。
這是我最終解決方案的代碼。
我更換:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
附:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
if let navController = self.navigationController {
for controller in navController.viewControllers {
if controller is TableViewController {
navController.popToViewController(controller, animated: true)
}
}
}
} else {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
因此,現在單擊“ Main”時,將所有以前的控制器彈出到“ TableViewController”,而不是從一個控制器到另一個控制器的推送而不將它們中的任何一個彈出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.