Last active
January 25, 2017 07:47
-
-
Save Nicholas-Swift/63b3da42e174a34904b1213e6c229e1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BadTableViewController.swift | |
// TableViewMemoryManagement | |
// | |
// Created by Nicholas Swift on 1/24/17. | |
// Copyright © 2017 Nicholas Swift. All rights reserved. | |
// | |
import UIKit | |
class BadTableViewController: UIViewController { | |
// MARK: - Instance Vars | |
@IBOutlet weak var tableView: UITableView! | |
// NOTE: - Creating a var to hold all cells is bad, takes up too much memory | |
var cells: [UITableViewCell] = [] | |
// MARK: - View Lifecycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
cellsSetup() | |
tableViewSetup() | |
} | |
} | |
// MARK: - Setup | |
extension BadTableViewController { | |
// NOTE: - Here we pre-create all the cells we need and add them to the cells array. Not good! | |
func cellsSetup() { | |
for _ in 0...1000 { | |
let cell = UITableViewCell() | |
cell.textLabel?.text = "Wow so bad" | |
cells.append(cell) | |
} | |
} | |
func tableViewSetup() { | |
tableView.rowHeight = UITableViewAutomaticDimension | |
tableView.estimatedRowHeight = 350 | |
} | |
} | |
// MARK: - Table View Delegate | |
extension BadTableViewController: UITableViewDelegate { | |
} | |
// MARK: - Table View Data Source | |
extension BadTableViewController: UITableViewDataSource { | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return cells.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
// NOTE: - Now we only need to specify a cell from the array based on index, but not worth it | |
let cell = cells[indexPath.row] | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment