I have set up a UITableView
in which each cell has a child UIViewController
I've tried reusing cells using dequeueReusableCell(with identifier:)
and adding/removing child view controller on willDisplay cell
and didEndDisplaying cell
methods.
Another thing I preferred using was not dequeuing cells and storing/accessing them like this:
let cell: FeedCell
if let cached = feedCells[feedItem.uuid]{
cell = cached
}else{
cell = Bundle.main.loadNibNamed("FeedCell", owner: nil, options: nil)?.first as! FeedCell
cell.feedItem = feedItem
feedCells[feedItem.uuid] = cell
}
So basically the property feedCells
is a dictionary [String: FeedCell]
, when I was reusing view controllers, it was [String: UIViewController]
I've identified that the key to my issue is this dictionary and didReceiveMemoryWarning
Whenever I receive a memory warning in the app, the visible cells or ViewControllers that are part of that dictionary get removed from view hierarchy even though they hold a strong
reference in the dictionary. They do persist to live in the dictionary so I can reload the table view and show them again. Which I'm doing now on didReceiveMemoryWarning
.
But my concern is why are the views being removed from the hierarchy like this even though we've held a strong reference to them, and any way to prevent this?