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