My situation is a bit more complex than what I've seen here before posting, and I'm not really good with memory management.
I have a custom UITableViewCell
(that we will call MyCell
here) and I pass its pointer to an UITableViewController
(MyController
here) when clicking on it. I pass the pointer because I want to call a method of this cell and the reference is only made by copy in Objective-C so it doesn't call the method on the right cell. I have made this:
MyController.h
@interface MyController : UITableViewController {
MyCell * __autoreleasing *_cell;
}
-(instancetype)initWithCell:(MyCell * __autoreleasing *)cell;
@end
MyController.m
- (instancetype)initWithCell:(MyCell **)cell {
if (self = [super init]) {
_cell = cell;
// Checkpoint 1
}
}
Then I want to use this variable later in my code, for example to define the number of sections:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Checkpoint 2
return (*_cell).contents.count; // contents being an NSArray property of the custom cell
}
The issue: At the "checkpoints" marked here, I have an NSLog(@"%ld", (unsigned long)(*_cell).contents.count);
, however it shows 2 (the right number) in the first checkpoint, but 0 in the second checkpoint, so basically when I click on the cell an empty table view is shown.
I used to pass the cell by copy by storing it in a nonatomic, strong
property and everything worked well, but by changing the calls from self.cell
to _cell
because of the pointer reference, the view is now empty as I said. It is likely a memory management issue, but I have no clue on how to solve it (fairly new to Objective-C, first app).
N.B.: I tried to change the __autoreleasing
by a __strong
, but this lead to a crash at every access of a property of the _cell
. I have also tried to use a nonatomic, assign
property to store it instead of using a ivar but it didn't solve my problem.
Thanks!
Edit: Forgot to mention that I call the view controller by using
[self.navigationController pushViewController:[[MyController alloc] initWithCell:(MyCell **)&cell] animated:YES];
in my previous view controller, in the tableView:didSelectRowAtIndexPath:
method.