Joyal Clifford Joyal Clifford - 6 months ago 10
Objective-C Question

How to Check If UITableViewCell Contains UIView

I am adding subview to

UITableViewCell
i.e

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault` reuseIdentifier:cellIdentifier];


Here
_cellBackgroundView
belongs to
UIView
.

[cell.contentView addSubview:_cellbackground];


I want to check by the help of
isDescendantOfView
if it contains
_cellbackground
, but I am getting warning.

if (![_cellbackground isDescendantOfView:[cell subviews]]) {
[cell.contentView addSubview:_cellbackground];

}
else{
[_cellbackground removeFromSuperview];
}


references Check if a subview is in a view

Please help

Answer

[cell subviews] return array , but you need to give UIView as input parameter for isDescendantOfView: method , try like this it will work

if (![_cellbackground isDescendantOfView:cell.contentView]) {
    [cell.contentView addSubview:_cellbackground];
}
else {
    [_cellbackground removeFromSuperview];
} 
Comments