cell怎么知道自己不在屏幕中了?(1)

昨天在群里看到这道面试题,感觉很有意思。今天先挖坑,记录部分需要考虑的情况,更复杂的条件以后再慢慢填。

我们先考虑cell在tableView中发生了滚动事件的情况。

简单的方案是监测tableView的滚动事件,然后通过visibleCells属性判断cell是否可见。

  • 成为某个视图的子视图时,先移除之前的kVO,然后对视图层级中所有class为UIScrollView的contentSize添加KVO
    - (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;
        }
      }
    
  • 记录KVO了哪些视图的,方便及时移除
    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];
      }
    
      - (void)unKVO {
        NSLog(@"");
        [receiverArr enumerateObjectsUsingBlock:^(id _Nonnull receiver, NSUInteger idx, BOOL *_Nonnull stop) {
          NSObject *observationInfo = [receiver observationInfo];
          NSArray *_observances = [observationInfo valueForKey:@"_observances"];
          [_observances enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
            NSObject *observer = [obj valueForKey:@"_observer"];
            if ([observer isEqual:self]) {
              NSString *_property = [obj valueForKeyPath:@"_property._keyPath"];
              [receiver removeObserver:self forKeyPath:_property];
            }
          }];
        }];
      }
    
  • 打印监测到的值变化
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context {

        NSLog(@"self:%@ visibleCells:%@", self, [[self tableView] visibleCells]);
    
        // 判断是否在tableView的可见cell内
        if (![[[self tableView] visibleCells] containsObject:self]) {
          NSLog(@"隐藏");
          self.textLabel.text = @"隐藏";
          NSLog(@"%@", [self tableView]);
        } else{
          NSLog(@"显示");
          self.textLabel.text = @"显示";
          NSLog(@"%@", [self tableView]);
        }
        NSLog(@"self:%@ visibleCells:%@", self, [[self tableView] visibleCells]);
      }
    

根据我在iPhone5S,iOS8.3上面的实测结果:满足离屏监测
第一种情况测试成功。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 5,870评论 0 3
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,873评论 0 9
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 5,272评论 0 3
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,391评论 30 472
  • 没有什么可以赠你 除了我自己 白云依依 芳草萋萋 我已在深谷里几世更替 鲜妍 摇落 化泥 拼却生生世世功力 竭尽美...
    黛眉居阅读 3,762评论 22 27

友情链接更多精彩内容