OGeek|极客世界-中国程序员成长平台

标题: ios - 如何检查 UITableViewCell 是否包含 UIView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 20:19
标题: ios - 如何检查 UITableViewCell 是否包含 UIView

我将 subview 添加到 UITableViewCell

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

这里_cellBackgroundView属于UIView

[cell.contentView addSubview:_cellbackground];

我想通过 isDescendantOfView 的帮助检查它是否包含 _cellbackground,但我收到了警告。

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

}
else{
    [_cellbackground removeFromSuperview];
}

引用 Check if a subview is in a view

请帮忙



Best Answer-推荐答案


[cell subviews] 返回数组,但是您需要将 UIView 作为 isDescendantOfView: 方法的输入参数,尝试这样它会工作

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

关于ios - 如何检查 UITableViewCell 是否包含 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37538974/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4