UICollectionReusableView 使用时的一些问题
2016-07-08 11:24
393 查看
在使用UICollectionView 时, 设置分区头时,
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
返回值不能为 nil
我开始写时, 写成下面样子,然后就报错
{
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;
}
- (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;
}
- (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;
}
相关文章推荐
- @value注解使用的简单介绍 ---谷营中西
- 数据类型变换uint8_t uint16_t等等
- Android 在子线程中更新UI的方式,使用handler
- Warning:The remote SSH server rejected X11 forwarding request.
- UICollectionViewLayout
- Errors running builder 'Validation' on project 'jumi_3.0'
- 点击tabBarItem跳转到其他界面 返回后还是之前所选item (跳转注册)
- php中生成标准uuid(guid)的方法
- error: static assertion failed: (boost::has_trivial_destructor<T>::value)
- non-zero exit value 1,non-zero exit value 2, non-zero exit value 3错误解决办法
- weblogic启动报错javax.xml.parsers.DocumentBuilderFactory
- GUI从入门到精通(一)
- UEditor图片不能居中,首行缩进无效等问题的解决
- 虚拟现实-VR-UE4-创建一个自定义的角色 Character
- 357. Count Numbers with Unique Digits
- ue4攻击目标的具体骨骼
- UITableViewCell
- UIImagePickerController详解
- Easyui datebox设置默认时间 和 时间范围(七天,一个月)
- 1122. Prerequisites?