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

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

#import <UIKit/UIKit.h>

@interface YimaiDoctorGroupMoreTopicCollectionView : UICollectionView

+ (instancetype)collectionViewWithObject:(id)object;

@end
#define kCollectionviewHeight 96.0
#define kCommonIdentifier @"YimaiDoctorGroupMoreTopicCommonCollectionCell"
#define kMyIdentifier @"YimaiDoctorGroupMoreTopicMyCollectionCell"
#import "YimaiDoctorGroupMoreTopicCollectionView.h" @implementation YimaiDoctorGroupMoreTopicCollectionView
static 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 = ;
layout.minimumInteritemSpacing = ; //初始化
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 ;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier;
if (indexPath.item < ) {
identifier = @"YimaiDoctorGroupMoreTopicCommonCollectionCell";
YimaiDoctorGroupMoreTopicCommonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.type = [NSString stringWithFormat:@"%ld",indexPath.item + ];
FLOG(@"cell ======== %@", cell);
return cell;
}else{
identifier = @"YimaiDoctorGroupMoreTopicMyCollectionCell";
YimaiDoctorGroupMoreTopicMyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.type = (int)indexPath.item + ;
return cell;
}
}

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

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

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

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

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

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier;
if (indexPath.item < ) {
identifier = [NSString stringWithFormat:@"%@-%ld",@"YimaiDoctorGroupMoreTopicCommonCollectionCell", indexPath.item];
YimaiDoctorGroupMoreTopicCommonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.type = [NSString stringWithFormat:@"%ld",indexPath.item + ];
FLOG(@"cell ======== %@", cell);
return cell;
}else{
identifier = @"YimaiDoctorGroupMoreTopicMyCollectionCell";
YimaiDoctorGroupMoreTopicMyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.type = (int)indexPath.item + ;
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 = ;
layout.minimumInteritemSpacing = ; //初始化
YimaiDoctorGroupMoreTopicCollectionView *collection = [[YimaiDoctorGroupMoreTopicCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
//注册
//解决ios9下 自定义的一个UICollectionViewCell只走一次awakeFromNib
for (int i = ; i < ; 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;
}

iOS9下UICollectionViewCell的awakeFromNib问题的更多相关文章

  1. iOS适配 旧项目工程在iOS9下不能正常显示

    在iOS开发中,很多时候会用到旧项目,比如版本的升级.使用Demo等等, iOS SDK正在不断的升级,不断的升级给iOS开发带来了新的活力. 然而在iOS SDK新的版本出来之后,旧项目可能会出现新 ...

  2. iOS9下的Map Kit View下的使用

    最近有个任务是关于地理位置上的标注开发,经过一些资料的查找和对比,现总结一些经验,给读者也是给自己. iOS9下的Map Kit View实际是以前MapKit,只不过换了一个名字,实际是指同一个UI ...

  3. iOS9下修改回HTTP模式进行网络请求

    升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错 The resource could not be loaded because the App Transport Sec ...

  4. iOS iOS9下修改回HTTP模式进行网络请求

    升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错 The resource could not be loaded because the App Transport Sec ...

  5. 解决IOS9 下在App中无法打开其他应用的问题

    打开 info.plist 文件 ,在根节点下添加下面代码即可,这是由于IOS9新的权限管理机制的问题 <key>LSApplicationQueriesSchemes</key&g ...

  6. iOS 检测有没有安装其它应用 和ios9下要注意的地方

    UIApplication *app = [UIApplication sharedApplication]; NSURL *url = [NSURL URLWithString:@"Tri ...

  7. ios9下ionic框架报[$rootScope:infdig] 10 $digest() iterations reached. Aborting!的解决办法

    升级ios9后,ionic开发的app会报[$rootScope:infdig] 10 $digest() iterations reached. Aborting!的错误,加上一个patch就可以解 ...

  8. xcode7和ios9下UIWebView不能加载网页的解决方法

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  9. 解决iOS9下隐藏App返回按钮文字导致的诡异闪屏问题

    问题的原因竟是一行代码导致的,这行代码的作用是隐藏App返回按钮的文字. 看看这有问题的代码: //将返回按钮的文字position设置不在屏幕上显示 [[UIBarButtonItem appear ...

随机推荐

  1. ORA-01795: maximum number of expressions in a list is 1000

     今天发现查询Oracle用In查询In的元素不可以超过1000个还需要分成多个1000查询记录博客备忘!! Load Test的时候发现这么如下这个错误.... ORA-01795: maximum ...

  2. How To Debug Qmake Pro File

    对于程序代码,我们经常使用到调试. 可是,对于有些项目的配置文件,比如Qt的Pro文件, 一个项目复杂的话,Pro文件就很容易出错. 此时的Pro文件,如果也能调试的话,那么是十分的快捷方便的. 解决 ...

  3. datagridview paging

    http://www.codeproject.com/Articles/211551/A-Simple-way-for-Paging-in-DataGridView-in-WinForm

  4. 随机分布 + action 计数

    For random samples from , use:  注意平方: sigma * np.random.randn(...) + mu 2.5*2.5 = 6.25 Two-by-four a ...

  5. [Jenkins]怎样在Jenkins上面启动服务器上的批处理脚本

    New Item 在Build --> Execute Windows batch command --> 里面填写: schtasks /run /tn Start_Hub_szotqa ...

  6. Java Thread系列(四)线程通信

    Java Thread系列(四)线程通信 一.传统通信 public static void main(String[] args) { //volatile实现两个线程间数据可见性 private ...

  7. Java Thread系列(一)线程创建

    Java Thread系列(一)线程创建 Java 中创建线程主要有三种方式:继承 Thread.实现 Runnable 接口.使用 ExecutorService.Callable.Future 实 ...

  8. 我读《从Paxos到zookeeper分布式一致性原理与实践》

    从年后拿到这本书开始阅读,到准备系统分析师考试之前,终于读完了一遍,对Zookeeper有了一个全面的认识,整本书从理论到应用再到细节的阐述,内容安排从逻辑性和实用性上都是很优秀的,对全面认识Zook ...

  9. 前端福利之个性化设置table的td宽度(总结)

    很多时候,我们在用到table时,都希望随意设置 每个单元格的宽度,而不希望单元格被内容撑开table的样式. 1.首先,设置table的宽度 width=“1000” 或者 width=“100%” ...

  10. windows7文件夹怎样默认图片大图显示?

    先打开一个含有图片的文件夹,在文件夹空白处右键选择属性,打开自定义选项卡. 确定自定义选项卡 显示的是:“优化此文件夹:图片”. 然后,选择:组织--文件夹和搜索选项--查看--文件夹视图,应用到文件 ...