I started a new project, and I chose to use ARC, but I’ve got a pretty weird issue.
I’ve got a subclass of UITableViewCell that has 2 sizes depending on wether the user long presses the cell or not.
When the size changes, it reveals another part of the cell with a different background that partly overlaps the actual cell background.
To do so, I have a property @property (strong, nonatomic, retain) UIImageView* optionsImageView;
I tried different stuff but they all lead to the same result, so I’ll just explain the one actually coded.
In the init method, I create the optionsImageView and adds it to self as flollows :
UIImage* image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sous_menu" ofType:@"png"]];
self.optionsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,
self.frame.size.height - 12,
self.frame.size.width,
image.size.height)];
optionsImageView.backgroundColor = [UIColor clearColor];
optionsImageView.image = image;
[self insertSubview:optionsImageView aboveSubview:self.backgroundView];
optionsImageView.alpha = 0;
When the user long presses the cell, I change the alpha of the optionsImageView.
But when I inspect it in the layoutSubviews method, optionImageView is nil, so are other members of my class.
#pragma mark - ACTIONS
- (void)hideOptionsPanel
{
NSLog(@"hideOptionsPanel for %@", titleLabel.text);
self._cellOptions = LRCellOptionsHidden;
NSLog(@"_cellOptions %d", self._cellOptions);
self.optionsImageView.alpha = 0;
}
- (void)showOptionsPanel
{
NSLog(@"showOptionsPanel for %@", titleLabel.text);
self._cellOptions = LRCellOptionsShown;
NSLog(@"_cellOptions %d (optionsImageView %@)", self._cellOptions, optionsImageView);
self.optionsImageView.alpha = 1;
}
- (void)layoutSubviews
{
NSLog(@"layoutSubviews for %@ (%d)", titleLabel.text, self._cellOptions);
// rounded corners
self.icon.layer.cornerRadius = 5.0;
self.icon.clipsToBounds = YES;
}
and here is the ouptput for all of this….
2011-12-24 14:11:31.788 LendReminder[11189:fb03] layoutSubviews for Cars 2 Bluray (0)
2011-12-24 14:11:32.137 LendReminder[11189:fb03] showOptionsPanel for Cars 2 Bluray
2011-12-24 14:11:32.138 LendReminder[11189:fb03] _cellOptions 1 (optionsImageView (null))
2011-12-24 14:11:32.140 LendReminder[11189:fb03] layoutSubviews for Cars 2 Bluray (0)
2011-12-24 14:11:32.141 LendReminder[11189:fb03] layoutSubviews for Cars 2 Bluray (0)
2011-12-24 14:11:33.442 LendReminder[11189:fb03] layoutSubviews for Sleepy Hollow (0)
2011-12-24 14:11:33.790 LendReminder[11189:fb03] hideOptionsPanel for Cars 2 Bluray
2011-12-24 14:11:33.791 LendReminder[11189:fb03] _cellOptions 0
2011-12-24 14:11:33.793 LendReminder[11189:fb03] showOptionsPanel for Sleepy Hollow
2011-12-24 14:11:33.793 LendReminder[11189:fb03] _cellOptions 1 (optionsImageView (null))
2011-12-24 14:11:33.796 LendReminder[11189:fb03] layoutSubviews for Sleepy Hollow (0)
2011-12-24 14:11:33.796 LendReminder[11189:fb03] layoutSubviews for Cars 2 Bluray (0)
2011-12-24 14:11:33.797 LendReminder[11189:fb03] layoutSubviews for Sleepy Hollow (0)
You can see that in the showOptionsPanel and HideOptionPanel, the option Enum is ok whilst in the layoutSubviews, it is still equal to 0 and the optionView is nil…
What the hell? Is this something that has to do with ARC or what?
Thx.
I got it ;))
I did not set the reuseIdentifier in the xib, so each time I called cellAtIndexPath, the cell was rebuild, hence nil pointers. I suck I know 😀 😀 😀