您的位置:首页 > 产品设计 > UI/UE

UICollectionReusableView 使用时的一些问题

2016-07-08 11:24 393 查看
在使用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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: