如题。我希望UICollectionView中实现类似于UITableView的tableHeaderView,怎么做啊
如题。我希望UICollectionView中实现类似于UITableView的tableHeaderView,怎么做啊
如果只是一个 header 一个 footer,实现- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
即可。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
reusableview = someHeaderView;
}
if (kind == UICollectionElementKindSectionFooter) {
reusableview = someFooterView;
}
return reusableview;
}
如果是要保持停留在顶部的效果,没有直接能设置的办法,只能自己实现。
有一些第三方库可以实现,也可以参考这里。
2 回答1.1k 阅读
1 回答1k 阅读✓ 已解决
1 回答2.7k 阅读
1 回答1.4k 阅读
1.7k 阅读
1 回答1.1k 阅读
1.3k 阅读
在初始化时和添加cell类似
[collectionView registerClass:[HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeadID];
再加楼上的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
HeadView * view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeadID forIndexPath:indexPath];
return view;
}
return nil;
}