博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS9下UICollectionViewCell的awakeFromNib问题
阅读量:5033 次
发布时间:2019-06-12

本文共 5883 字,大约阅读时间需要 19 分钟。

  最近项目测试出一个隐藏已久的bug,经过多番测试,发现在iOS9下自定义的一个UICollectionViewCell只走一次awakeFromNib。

  具体情况是,项目中有一个控制器用到了自定义的UICollectionView,有四组数据,然而有三组自定义的UICollectionViewCell布局和子控件是一样的,所以只注册了两种类型的cell

#import 
@interface YimaiDoctorGroupMoreTopicCollectionView : UICollectionView+ (instancetype)collectionViewWithObject:(id)object;@end
#define kCollectionviewHeight 96.0#define kCommonIdentifier @"YimaiDoctorGroupMoreTopicCommonCollectionCell"#define kMyIdentifier @"YimaiDoctorGroupMoreTopicMyCollectionCell"#import "YimaiDoctorGroupMoreTopicCollectionView.h"@implementation YimaiDoctorGroupMoreTopicCollectionViewstatic NSString const *commonIdentifier = @"YimaiDoctorGroupMoreTopicCommonCollectionCell";static NSString const *myIdentifier = @"YimaiDoctorGroupMoreTopicMyCollectionCell";+ (instancetype)collectionViewWithObject:(id)object{    //流水布局    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;    CGFloat itemHeight = kScreenH - kCollectionviewHeight - kNavigtaionHeight;    layout.itemSize = CGSizeMake(kScreenW, itemHeight);    layout.minimumLineSpacing = 0;    layout.minimumInteritemSpacing = 0;            //初始化    YimaiDoctorGroupMoreTopicCollectionView *collection = [[YimaiDoctorGroupMoreTopicCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];    //注册   [collection registerNib:[UINib nibWithNibName:kCommonIdentifier bundle:nil] forCellWithReuseIdentifier:kCommonIdentifier];    [collection registerNib:[UINib nibWithNibName:kMyIdentifier bundle:nil] forCellWithReuseIdentifier:kMyIdentifier];    //分页    collection.pagingEnabled = YES;    //数据源,代理    collection.delegate = object;    collection.dataSource = object;    //    collection.backgroundColor = [UIColor clearColor];    return collection;}@end

在所属控制器的数据源方法中,初始化和赋值

#pragma mark - UICollectionViewDelegate- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 4;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    static NSString *identifier;    if (indexPath.item < 3) {        identifier = @"YimaiDoctorGroupMoreTopicCommonCollectionCell";        YimaiDoctorGroupMoreTopicCommonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];        cell.type = [NSString stringWithFormat:@"%ld",indexPath.item + 1];        FLOG(@"cell ======== %@", cell);        return cell;    }else{        identifier = @"YimaiDoctorGroupMoreTopicMyCollectionCell";        YimaiDoctorGroupMoreTopicMyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];        cell.type = (int)indexPath.item + 1;        return cell;    }}

每个自定义的UICollectionViewCell里面添加了UITableView作为子控件,并且能上拉加载,下拉刷新,所以有一些初始化

#pragma mark - life cycle- (void)awakeFromNib {    [super awakeFromNib];    //设置tableview    [self setTableViewAttributes];    self.page = 1;}

当前控制器里顶部还加了资格按钮,用来点击可以实现UICollectionView的滑动

上面是核心源码,看似很正确的实现,最后却验证在iOS9下“不能滑动”,即看似下面这句话不执行

[self.collectionView setContentOffset:CGPointMake(index * kScreenW, 0) animated:NO];

最后通过各种系统版本机型的调试,验证了,上面的这句setContentOffset没有问题,有问题的是,iOS9上YimaiDoctorGroupMoreTopicCommonCollectionCell的awakeFromNib方法只执行了一次,而在其他的手机版本下走了三次,所以导致当初注册的两种类型的cell,这里三种看似相同的cell现在都是完全一样了,数据都一样。类似于是用的同一个地址,而不是我们认为的,三种长得一样的cell都拥有自己独立的地址,所以才导致看着是没滑动,具体为什么还在不同的系统版本上有这种情况,查了资料也没什么头绪,有了解的伙伴们还请多多指点!!最后只附上解决方法:三种看似相同的cell不复用了,于是有了下面重用标识符的修改

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    static NSString *identifier;    if (indexPath.item < 3) {        identifier = [NSString stringWithFormat:@"%@-%ld",@"YimaiDoctorGroupMoreTopicCommonCollectionCell", indexPath.item];        YimaiDoctorGroupMoreTopicCommonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];        cell.type = [NSString stringWithFormat:@"%ld",indexPath.item + 1];        FLOG(@"cell ======== %@", cell);        return cell;    }else{        identifier = @"YimaiDoctorGroupMoreTopicMyCollectionCell";        YimaiDoctorGroupMoreTopicMyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];        cell.type = (int)indexPath.item + 1;        return cell;    }}
+ (instancetype)collectionViewWithObject:(id)object{    //流水布局    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;    CGFloat itemHeight = kScreenH - kCollectionviewHeight - kNavigtaionHeight;    layout.itemSize = CGSizeMake(kScreenW, itemHeight);    layout.minimumLineSpacing = 0;    layout.minimumInteritemSpacing = 0;            //初始化    YimaiDoctorGroupMoreTopicCollectionView *collection = [[YimaiDoctorGroupMoreTopicCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];    //注册    //解决ios9下 自定义的一个UICollectionViewCell只走一次awakeFromNib    for (int i = 0; i < 3; i++) {        [collection registerNib:[UINib nibWithNibName:kCommonIdentifier bundle:nil] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@-%d", @"YimaiDoctorGroupMoreTopicCommonCollectionCell", i]];    }//    [collection registerNib:[UINib nibWithNibName:kCommonIdentifier bundle:nil] forCellWithReuseIdentifier:kCommonIdentifier];    [collection registerNib:[UINib nibWithNibName:kMyIdentifier bundle:nil] forCellWithReuseIdentifier:kMyIdentifier];    //分页    collection.pagingEnabled = YES;    //数据源,代理    collection.delegate = object;    collection.dataSource = object;    //    collection.backgroundColor = [UIColor clearColor];    return collection;}

 

转载于:https://www.cnblogs.com/jingxin1992/p/9013298.html

你可能感兴趣的文章
我对你的期望有点过了
查看>>
微信小程序wx:key以及wx:key=" *this"详解:
查看>>
下拉框比较符
查看>>
2.2.5 因子的使用
查看>>
css选择器
查看>>
photoplus
查看>>
Python 拓展之推导式
查看>>
[Leetcode] DP-- 474. Ones and Zeroes
查看>>
80X86寄存器详解<转载>
查看>>
c# aop讲解
查看>>
iterable与iterator
查看>>
返回顶部(动画)
查看>>
webpack+react+antd 单页面应用实例
查看>>
Confluence 6 SQL Server 数据库驱动修改
查看>>
Confluence 6 通过 SSL 或 HTTPS 运行 - 备注和问题解决
查看>>
【47.76%】【Round #380B】Spotlights
查看>>
Git(使用码云)
查看>>
分享Java web 开发必游之路
查看>>
IIS初始化(预加载),解决第一次访问慢,程序池被回收问题(转载)
查看>>
Bean的Scope
查看>>