iphone - 如何从 UITableViewCell 获取 UITableView?

标签 iphone ios objective-c xcode tableview

我有一个 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()
}

关于iphone - 如何从 UITableViewCell 获取 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15711645/

相关文章:

iphone - 在两个标签之间保持 map 同步

ios - UINavigationBar 元素太靠近 UIStatusBar

ios - Xcode - 如何修复 'NSUnknownKeyException' ,原因 : … this class is not key value coding-compliant for the key X"error?

ios - 如何防止mapview被释放?

objective-c - NSMutableArray 中对象的生命周期

iphone - 如何在 iOS 中勾选 UIButton?

ios - 如何从我的 iOS 应用程序打开带有最近通话摘要的 iPhone 手机应用程序?

ios - iPad2 复制 1K 图像需要 0.5 秒——这是我能做的最好的吗?

ios - 如何删除所有具有相同属性值但在 NSMutableArray 中有一个的对象

iphone - UIToolbar 具有半透明而不是黑色