怎么判断某个cell是否显示在屏幕上

2026-02-14 09:08:19

1、首先,在成为某个视图的子视图时,先移除之前的kVO,然后对视图层级中所有class为UIScrollView的contentSize添加KVO。

怎么判断某个cell是否显示在屏幕上

2、然后,输入程序- (void)didMoveToSuperview {

    [self unKVO];

    UIView *aView = self.superview;

    while (aView) {

      if ([aView isKindOfClass:[UIScrollView class]]) {

        [self observer:aView forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:(void *)__LINE__];

      } aView = aView.superview;   } }

怎么判断某个cell是否显示在屏幕上

3、然后,需要设置TableViewseparatorStyle属性UITableViewCellSeparatorStyleNone即除划。代码:tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

怎么判断某个cell是否显示在屏幕上

4、当然,如果记录KVO了哪些视图的,方便及时移除:

- (BOOL)isDisplayedInScreen

{ if (self == nil) { return FALSE; }CGRect screenRect = [UIScreen mainScreen].bounds;

怎么判断某个cell是否显示在屏幕上

5、然后,打印监测到的值变化:

static NSMutableArray *receiverArr;  static SunTableViewCell *cell;  - (void)observer:(NSObject *)receiver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context {    if (cell && cell != self) {      return;    }    cell = self;    [receiver addObserver:self forKeyPath:keyPath options:options context:context];    if (!receiverArr) {      receiverArr = [NSMutableArray array];    }    [receiverArr addObject:receiver];  }  - (void)dealloc {    [self unKVO];

怎么判断某个cell是否显示在屏幕上

6、最后,获取 该view与window 交叉的 Rect

    CGRect intersectionRect = CGRectIntersection(rect, screenRect);

    if (CGRectIsEmpty(intersectionRect) || CGRectIsNull(intersectionRect)) {

 return FALSE; } return TRUE;}

怎么判断某个cell是否显示在屏幕上

猜你喜欢