Skip to content

Instantly share code, notes, and snippets.

@uc-compass-bot
Created October 15, 2019 14:19
Show Gist options
  • Save uc-compass-bot/a5b54a7042e1b1d3ea6890a7873a32ec to your computer and use it in GitHub Desktop.
Save uc-compass-bot/a5b54a7042e1b1d3ea6890a7873a32ec to your computer and use it in GitHub Desktop.
Catch Retain Cycles In Table View Subclass
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