I am really stuck in this issue for quite a long time
I am trying to add a UIControl
(which is a UIView
in the end) to a UITableViewCell
that i have subclasses in my own class (i made a custom cell)
on swipe, i create my UIControl
class and add it to myself (the cell), so far so good. Here is the code
[self addSubview:_statusView];
However, i am adding a target action to my UIControl
in the custom cell, so that the cell can handle when the UIControl
says that he has recognized a touchDownEvent.
[self.statusView addTarget:self action:@selector(resetAll:) forControlEvents:UIControlEventTouchDown];
And here is what i want to do in the action, I want to remove that UIControl
from self.subviews
(the cell's subviews), so i set the action method to be like this
- (void)resetAll:(id)sender
{
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[StatusView class]]) {
[view removeFromSuperview];
}
}
}
Can someone point out whats wrong in this code? because i can't really figure out why the view that gets added to the cell does't get removed. It seem to me the the subviews property doesn't ever contain my UIControl
that i add.
UITableViewCell
internally does some manipulations with its view hierarchy. You should add subviews not to the cell itself, but to its contentView
, as stated in the docs:
If you want to go beyond the predefined styles, you can add subviews to the contentView property of the cell.
So you have to replace
[self addSubview:_statusView];
with
[self.contentView addSubview:_statusView];
And then iterate on subviews of the contentView
:
for (UIView *view in self.contentView.subviews) {
if ([view isKindOfClass:[StatusView class]]) {
[view removeFromSuperview];
}
}