UICollectionView嵌套TableView时的注意事项

开发中,需要实现三个水平滑动页面,其中一个页面是UITableView,页面需要每个2秒刷新一次,另外两个是普通UIView.
实现思路如下:三个页面的水平滑动用UICollectionView实现,其中的UITabelView嵌套在UICollectionCell的contentView里面,其刷新用NSTimer来实现.
下面是tableView中的代码

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self timer];
    }
    return self;
}
- (void)dealloc{
    [self.timer invalidate];
    self.timer = nil;
}
- (NSTimer *)timer{
    if (_timer == nil) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerDid) userInfo:nil repeats:YES];
    }
    return _timer;
}
- (void)timerDid{
    [self reloadData];
    NSLog(@"timer");
}

根据实际测试,当在滑动collectionView时,并不会调用tableView的dealloc方法,并且控制台一直打印timer的信息.也就是说timer对象和tableView对象并不会销毁.这是因为collectionView在滑动时的重用机制造成的,放入缓存池中的collectionViewCell对象并没有销毁,也就不会调用dealloc方法,而timer执行的方法又有对self的引用,所以tableView并没有销毁.当用户反复滑动collectionView时,会多次创建tableView,造成大量的资源浪费,降低手机性能.
解决方法,是在collectionView中的tableView页面进入缓存池后,手动调用 [self.timer invalidate]方法,强行终止timer,collectionView中的方法如下

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        //将cell重用时的其他控件移除
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//        cell.contentView.backgroundColor = [UIColor whiteColor];
        ZDTabelView *tableView = [[ZDTabelView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
        tableView.backgroundColor = [UIColor greenColor];
        self.tableView = tableView;
        [cell.contentView addSubview:tableView];
        
    }else if(indexPath.row == 1){
        [self.tableView.timer invalidate];
        self.tableView.timer = nil;
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        cell.contentView.backgroundColor = [UIColor grayColor];
    }else{
        [self.tableView.timer invalidate];
        self.tableView.timer = nil;
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        cell.contentView.backgroundColor = [UIColor redColor];
    }
    return cell;
}

Tips:在调用timer的invalidate方法后,timer有可能并没有变成nil,所以需要显式的指定.
下面是github上的Demo:

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

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,667评论 4 61
  • 在很多面试题中,经常会看到关于变量提升,还有函数提升的题目,所以我就写一篇自己理解之后的随笔,方便之后的查阅和复习...
    McRay阅读 2,870评论 0 1
  • 金钱,欲望,青春,角色,爱,消磨 一个为爱冲动,和恋人离家出走的姑娘,在经历了无数人的求爱,美丽的衣服,听话的侍从...
    微尘众33阅读 4,288评论 1 0
  • 《吕氏春秋》里有一则《贵公》,大约是晚出的缘故,有的论据和论证指向儒家思想,有的又暗暗透出道家风味。 其实公正,最...
    小禾小珩妈阅读 3,379评论 0 2
  • 对清华的学生一直抱有一种神秘感,虽说自己的学校也算是名校,可是跟清华比起来就会被藐几条街。说来惭愧,自己的同学中还...
    Eelizabeth_Li阅读 2,915评论 1 2

友情链接更多精彩内容