Could someone explain to me why this works :
SpecialView *view = [[SpecialView alloc]initWithFrame:CGRectMake(0, 0, 320, 164)];
self.specialView = view;
self.tableView.tableHeaderView = self.specialView;
And this doesn’t :
self.specialView = [[SpecialView alloc]initWithFrame:CGRectMake(0, 0, 320, 164)];
self.tableView.tableHeaderView = self.specialView;
Thank you very much !
EDIT 1 : The property in .h is like :
@property (weak, nonatomic) SpecialView *specialView;
When I mean it doesn’t work, I mean the at the end the self.specialView is nil.
(And yes I’m using ARC)
Your property is defined as
weak. This means the reference isn’t retained. It also means that when the object is deallocated, the property will be set tonil.In the first bit of code you assign the
SpecialViewinstance to a local (strong) variable. This keeps the object around for a bit. Then you assign the instance to the (weak) property. This doesn’t help any. But then you assign the weak property to the table view’s header. It’s this extra reference that keeps the instance alive after the local variableviewgoes out of scope. But if you were to assign another header to the table or if the table view went away, the special view would be released and deallocated and the property would be reset to nil.In the second bit of code, you don’t have the local variable. So the object gets released and deallocated immediately and the property gets reset to
nil.Most likely your property needs to be defined as
stronginstead ofweak.