2

Sorry if this has been asked already but I've searched and haven't found an answer to the specific case I'm finding myself in right now.

I'm subclassing UITableViewController. I don't have a custom nib for it. I also have a custom tableview cell that I'm implementing that does have it's own custom nib. When I'm creating the outlet for this cell should I be creating a weak reference? Because it ends up as a subview of the TableViewController I would think yes but the Apple docs also note that top level objects in a nib should be strong so I'm confused. Any help would be appreicated!

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
jkratz
  • 393
  • 2
  • 11
  • 1
    possible duplicate of [Should IBOutlets be strong or weak under ARC?](http://stackoverflow.com/questions/7678469/should-iboutlets-be-strong-or-weak-under-arc) – Steve Feb 27 '12 at 00:07
  • Definitely a duplicate of the post above. – Steve Feb 27 '12 at 00:07
  • Because it's a custom nib for a tableview cell, and not for a UIViewController, I think this question has some merit and should not be closed. – Emile Cormier Feb 27 '12 at 00:24
  • Thanks Emile. I posted because its *not* a dupe. That question didnt ask exactly what I asked which is why I posted. – jkratz Feb 27 '12 at 00:28
  • Aren't cells memory managed by the UITableView? I don't see why you would have an outlet for the cell in the controller. Subclass or no subclass. – Minthos Nov 28 '12 at 19:43

4 Answers4

1

Use a weak reference. This is because when your view is unloaded, if it is weak the link will automatically be set to nil.

charleyh
  • 2,033
  • 2
  • 20
  • 32
0

"Apple docs also note that top level objects in a nib should be strong so I'm confused"

This is true no matter what UIView subclass you're using. If you have a nib with a UITableViewCell and a UIView IBOutlet, both as top level objects, the UIView needs to be strong. if the UIView IBOutlet is a subview of your UITableViewCell in the nib, it should be weak.

Jeremy Z
  • 106
  • 2
  • 8
0

You must use weak ownership.Because your table view cell have a strong reference to it's parent view and your parent view have a strong reference to it's subviews

Like this:

@property (weak, nonatomic) IBOutlet MyView *viewContainerSubview;
@property (strong, nonatomic) IBOutlet UIView *topLevelView;
Narek Safaryan
  • 140
  • 1
  • 7
0

If you put the cell in the tableview only outside of the xib (and I'm pretty sure you would be), then it needs to be a strong reference or else the cell will be released before you can use it in constructing the table.

Possibly if you made use of it in viewDidLoad it would still exist, but best not to rely on the call cycle acting in that way.

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • "If you put the cell in the tableview only outside of the xib (and I'm pretty sure you would be)". I assume you mean programmatically. I'm loading the UITableViewCell nib in the call to tableView: cellForRowAtIndexPath: where i'm configuring the cell as required. – jkratz Feb 27 '12 at 02:35
  • That's pretty much what I meant. If your reference is weak then the cell will be autoreleased before the code reaches that method (or I think it should be). – Kendall Helmstetter Gelner Feb 27 '12 at 03:34