Created
October 15, 2019 14:19
-
-
Save uc-compass-bot/a5b54a7042e1b1d3ea6890a7873a32ec to your computer and use it in GitHub Desktop.
Catch Retain Cycles In Table View Subclass
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
import UIKit | |
class TableView: UITableView { | |
private var allCells: [WeakBox<UITableViewCell>] = [] | |
private func appendIfNotPresent(_ cell: UITableViewCell) { | |
if allCells.first(where: { $0.unbox === cell }) == nil { | |
allCells.append(WeakBox(cell)) | |
} | |
} | |
override func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell { | |
let cell = super.dequeueReusableCell(withIdentifier: identifier, for: indexPath) | |
appendIfNotPresent(cell) | |
return cell | |
} | |
deinit { | |
allCells.forEach { MemoryChecker.verifyDealloc(object: $0.unbox) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment