OGeek|极客世界-中国程序员成长平台

标题: ios - 内存使用量不断增加 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:32
标题: ios - 内存使用量不断增加

我创建了一个非常简单的测试应用程序。它包括 3 个 View Controller 。主视图 Controller 有一个表格 View ,它使用自定义单元格。另外两个 View Controller 可以通过主视图 Controller 访问,每个都有 Collection View ,可以回到主视图 Controller 。

这是内存问题。每当我单击 3 个 View Controller 中的任何单元格时,内存使用量都会增加。我在使用“泄漏”分析模板时运行了该应用程序,但没有发现任何泄漏。还使用了'Allocations'分析模板,检查了2个 View Controller (记录了引用计数),我的程序下所有存储的引用计数都被释放了。

我无法使用 Debug Memory Graph,因为它不断使 Xcode 崩溃...

主视图 Controller

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)
    }
}

其他 View Controller 之一(都相同)

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
    }

}

如果需要,我会提供任何其他图片。任何帮助将不胜感激。

我使用分配工具获得的信息。 Possible Memory Leak 注释掉的代码只是我不需要atm的搜索功能。

Storyboard



Best Answer-推荐答案


问题似乎出在 CustomCell 内部,可能是某些资源未初始化。 awakeFromNib()setSelected(...) 中是否有一些代码?

关于ios - 内存使用量不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43528592/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4