ios - 如何检查 UITableViewCell 是否包含 UIView

标签 ios objective-c uitableview uiview

我正在向 UITableViewCell 添加 subview ,即

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

请帮忙

最佳答案

[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/

相关文章:

iphone - NSFetchedResultsController : Fill TableView with fetched entity and its related entity

ios - 如何取消或停止来自客户端的推送通知?

ios - value 不为零,但在解包可选值时意外发现 nil

objective-c - 如何在 applicationWillFinishLaunching 委托(delegate)中终止 cocoa 应用程序

ios - AVAudioRecorder 和 AirPlay 镜像

ios - 为什么除非我使用调度主队列,否则 PopViewControllerAnimated 会崩溃?

ios - 如何在 Objective-C 中将 IP 地址从 NSString 转换为 unsigned int?

Javascript 分析 iOS 8 Safari

ios - swift MapKit : Calculate ETA for a specific Route and not for directions?

iOS:scrollToRowAtIndexPath 不显示最后一页的正确顶部索引路径