iphone - 如何从 UITableViewCell 获取 UITableView?

我有一个 UITableViewCell 链接到一个对象,我需要判断单元格是否可见。根据我所做的研究,这意味着我需要以某种方式访问​​包含它的 UITableView (从那里,有几种方法可以检查它是否可见)。所以我想知道 UITableViewCell 是否有指向 UITableView 的指针,或者是否有任何其他方法可以从单元格中获取指针?

最佳答案

为了避免检查 iOS 版本,迭代地从单元格的 View 向上遍历 super View ,直到找到 UITableView:

Objective-C

id view = [cellInstance superview];

while (view && [view isKindOfClass:[UITableView class]] == NO) {
    view = [view superview]; 
}

UITableView *tableView = (UITableView *)view;

swift

var view = cellInstance.superview
while (view != nil && (view as? UITableView) == nil) {
  view = view?.superview
}
        
if let tableView = view as? UITableView {
   tableView.beginUpdates()
   tableView.endUpdates()
}

https://stackoverflow.com/questions/15711645/

相关文章:

ios - 如何拦截 MKMapView 或 UIWebView 对象上的触摸事件?

iphone - presentModalViewController :Animated is d

ios - 带有固定部分标题的 UITableView

objective-c - iOS:比较两个日期

objective-c - 获取对 iOS 应用程序中最顶层 View /窗口的引用

ios - 如何屏蔽 UIImageView?

iphone - 如何获得精确的时间,例如在 Objective-C 中以毫秒为单位?

objective-c - 更改 UIButton 文本

objective-c - iPad 纵向和横向模式的尺寸调整类

ios - 了解 NSRunLoop