1

I am working with the application and want to remove table view when view disappear and want to add that same table view when view appear.

Currently

  1. From A ctr(push) -> B ctr

  2. From B ctr(push) -> C ctr -- I removed tableview in B ctr from superview in viewwilldisappear -- works fine.

  3. From C ctr(pop) -> B ctr -- In view will appear of B ctr I added table view again. Viewwillaprea get called but tableview is not added as subview.

4 From B ctr(pop) -> A ctr

  1. From A ctr(push) -> B ctr -- This time in B ctr tableview appears.

It seems like when I come back with popViewControllerAnimated its not adding table as subview.

I have table in xib.

I want to do this in order to release the allocated memory.

tableview outlet

 @property (nonatomic,weak) IBOutlet UITableView *tView;

Remove table view in viewWillDisappear

  [self.tView removeFromSuperview];

Adding back in viewWillAppear

  [self.view addSubview:self.tView];

Thanks in advance

Satish
  • 1,012
  • 2
  • 15
  • 32
  • Why do you want to do that? Show the code. Most likely you destroy the table view and don't recreate it... – Wain May 16 '15 at 10:34
  • Hello Wain, Thanks for reply, I want to do this in order to release the allocated memory. I just edited question with code.. – Satish May 16 '15 at 10:40

2 Answers2

1

You shouldn't worry about this unless you have reason to - i.e. repeated memory warnings. And, if you do that those you should work out why - it's highly unlikely to be the table view. If anything it may be the data you load to populate the table view, so you could think about removing that data when not on display...

Anyway, your issue is that you destroy and don't recreate the table view. Your property:

 @property (nonatomic,weak) IBOutlet UITableView *tView;

is weak, so when you remove the view from its superview it is deallocated because nothing else is holding a reference to it.

When you later call:

 [self.view addSubview:self.tView];

you need to have recreated the table view and added it to the subview before you call self.tView = newTView because otherwise nothing will be retaining it again and it would be destroyed before you get to use it.

Key points:

  1. fix any memory issue (profile to find the true cause)
  2. don't remove the table view
Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks Wain for quick reply, really appreciate your help.. I did googled to find out the solution to resolve the memory issue.. tried few but non helped... Removing table view though does release the allocated memory... also as you can see when i again push the view it loads the table.. Isn't there any way i can re-add it as it does when pushed... – Satish May 16 '15 at 10:54
  • Presumably you have a storyboard? So no, not really. Other than popping when you go to C and actually pushing, but this isn't trivial as it isn't designed like that. REALLY - don't remove the view, find the real problem – Wain May 16 '15 at 10:56
  • The view controller is in the XIB? Same problem. If only the table view is in the XIB then you can reload it. But seriously, this is not the correct solution to your problem – Wain May 16 '15 at 10:59
  • Set the data to nil / row count to zero and reload data – Wain May 16 '15 at 11:08
0

You have set the weak property attribute for the UITableView. The moment you remove it from superview, it will be removed from memory.

You can set the strong attribute to prevent this from happening. But that would defeat the point of removing it for the purposes of freeing memory.

You can create a new tableView and set it in the -viewWillAppear method.

To know more about these attributes check the documentation.

Also this article which explains the various property attributes well.

ZeMoon
  • 20,054
  • 5
  • 57
  • 98