0

I have a TableViewController inside a PageViewController, pushed into a NavigationController. When I tap Back to unload the PageViewController I see the PageViewController deallocated, then the TableViewController deallocated, but not the TableView cells.

I've made the TableViewCell as simple as possible...

#import "MyTableViewCell.h"

@implementation MyTableViewCell

- (void)awakeFromNib {
    NSLog(@"Created");
}

- (void)dealloc
{
    NSLog(@"DEALLOCed");
}

@end

...but still only see the Created message for the TableViewCell.

And this is how I load my cells...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"EmptyCell";

    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (!cell) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    return cell;
}

Any ideas what may be causing the TableView cells to stay in memory? And am I right in assuming the TableViewController and PageViewController can't be the problem if the dealloc methods are calling on them?

bcl
  • 478
  • 1
  • 5
  • 16
  • One of two possibilities: 1) Your custom class is not being used by the table view, but rather UITableViewCell is being used. 2) Something in your logic is retaining the cells. – Hot Licks Mar 09 '15 at 00:32
  • Can you show some code how you are loading the cells? – gagarwal Mar 09 '15 at 04:43
  • @HotLicks: I'm presuming my custom class is being used as I see the Created message written to the console, just not the Dealloc message. – bcl Mar 09 '15 at 14:48
  • @gagarwal : Have added the load code cheers. – bcl Mar 09 '15 at 14:49
  • Why do you need to follow up `dequeueReusableCellWithIdentifier:forIndexPath:` with a check for nil and an alloc/init? It always returns a valid cell. (Did you register your cell class??) – Hot Licks Mar 09 '15 at 17:57
  • @hotlicks I've just always seen that done in examples and assumed it was necessary. Will happily remove if it's not required. I've registered the cell with an identifier in Interface Builder not registered the cell class in code. Would it be better if I did the latter? – bcl Mar 09 '15 at 19:11
  • 1
    The examples you were looking at were (hopefully) using the old `dequeue...` operation that lacked `forIndexPath:`. (If they weren't then don't use those examples, or any others from that source!!) The old operation did not automatically create new cells when there were none already cached. – Hot Licks Mar 09 '15 at 19:17
  • @HotLicks Cheers- I made the change and it obviously didn't affect the original issue but was good to know. – bcl Mar 10 '15 at 10:37
  • **For deallocation Just Visit Solution is here** **https://stackoverflow.com/a/72713841/10632772** – Asfar Hussain Siddiqui Jun 22 '22 at 10:36

1 Answers1

0

Turns out the problem was a class I made for displaying messages when the TableView is empty was retaining a reference to the TableView Controller. Made this weak and problem sorted :)

bcl
  • 478
  • 1
  • 5
  • 16