在使用UICollectionView 时, 设置分区头时,

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

返回值不能为 nil

我开始写时, 写成下面样子,然后就报错

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

if ([kind isEqualToString:UICollectionElementKindSectionHeader])

{

if (indexPath.section == 0)

{

HomeCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:firstReusableView forIndexPath:indexPath];

return view;

}

else

{

HomeCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:secondReusableView forIndexPath:indexPath];

return view;

}

}

return nil;

}

报错信息:
*** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-2903.2/UICollectionView.m:1401 后来多方查找原因, 发现, 以上方法, 必须返回一个有效的值.

 正确的写法:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

UICollectionReusableView *view;

if ([kind isEqualToString:UICollectionElementKindSectionHeader])

{

if (indexPath.section == 0)

{

view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:firstReusableView forIndexPath:indexPath];

}

else

{

view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:secondReusableView forIndexPath:indexPath];

}

}

return view;

}

最新文章

  1. HDU2159 二维完全背包
  2. Web前端上万字的知识总结
  3. kafka java实例
  4. SQL Server 服务器器信息备份(一)--login新建脚本备份
  5. August 13th 2016 Week 33rd Saturday
  6. SQLServer系统监控
  7. 父<IFRAME>获取子页属性以及子页中<IFRAME>的方法
  8. dijkstra,bellman-ford,floyd分析比较
  9. 《JS权威指南学习总结--9.5 类和类型》
  10. D - 小晴天老师系列——晴天的后花园
  11. javascript实现图片的预览
  12. HTML入门教程,多年心血总结,一看就会
  13. SSH HTTP代理
  14. 微信小程序是怎么运行的?
  15. 基于diango简易BLOG图片预览
  16. postgresql某进程占用cpu资源过高,降不下来
  17. 内存布局:栈,堆,BSS段(静态区),代码段,数据段
  18. Jensen不等式
  19. 常用代码之五:RequireJS, 一个Define需要且只能有一个返回值/对象,一个JS文件里只能放一个Define.
  20. fatal One or more refs for names blocks change upload

热门文章

  1. 菜单栏被flex页面遮挡解决办法
  2. 一个非常牛比的前端google插件
  3. translate和replace的区别
  4. 使用canvas编写环形图.
  5. C语言scanf函数详细解释
  6. Summernote
  7. nyoj 142, poj 1039 ,hdu 1454 管道问题
  8. C——第一阶段结束
  9. Latex感想
  10. 串口 COM口 TTL RS-232 RS-485 区别 释疑