Mustafa
Mustafa

Reputation: 263

table view cell issue with dealloc from memory

I know my question is simple and I know many question on that topic but I can't find any solution to it.

I have simple table view only have the label for 100 rows. When I scroll the memory is increasing every time I scroll using the instrument tool in the Xcode although I use the dequeuing cell as apple documentation my question why the cells not releasing from memory (free from memory) every time I scroll it's creating new cell.

The following my code:

import UIKit

class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = "Table View Cell! "

        return cell

    }


}

You can check the instruments tools and you can see the memory increasing every time you scroll.

Please, how can I release cells from memory when I'm scrolling?

Thanks a lot.

Upvotes: 1

Views: 1103

Answers (1)

Asfar Hussain Siddiqui
Asfar Hussain Siddiqui

Reputation: 562

Simply you can deallocate memory from view controller's table view cell

When Back press clicked

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if self.isMovingFromParent {
        AllProductList.removeAll() 
        self.view = nil
    }
}

deinit {
    print("deinit called")
}
  1. Remove all array data model
  2. Remove parent view

Upvotes: 2

Related Questions